Help us shape the future of Go! Take the 2026 Go Developer Survey

gettokenbyserviceaccount

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 14 Imported by: 2

README

go-gettokenbyserviceaccount

MIT License

Overview

This is a Golang library to retrieve access token from Service Account of Google without using Google's OAuth2 package.

Usage

package main

import (
	"fmt"
	"log"

	"github.com/tanaikech/go-gettokenbyserviceaccount"
)

func main() {
	privateKey := "-----BEGIN PRIVATE KEY-----\n###-----END PRIVATE KEY-----\n"
	clientEmail := "###"
	scopes := "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/spreadsheets"
	impersonateEmail := ""
	res, err := gettokenbyserviceaccount.Do(privateKey, clientEmail, impersonateEmail, scopes)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.AccessToken) // In this case, the access token is retrieved.
}
  • privateKey, clientEmail, impersonateEmail and scopes are string values.

  • When you want to use multiple scopes, please put them separated by a space like "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/spreadsheets".

You can obtain the access token like below.

{
  "access_token": "#####",
  "expires_in": 3600,
  "token_type": "Bearer",
  "start_time": 1234567890,
  "end_time": 1234567890
}

You can also retrieve this result using Google's OAuth2 package. I created this library to study the JWT process.


Licence

MIT

Author

Tanaike

If you have any questions and commissions for me, feel free to tell me.

Update History

  • v1.0.0 (December 11, 2018)

    1. Initial release.
  • v1.0.1 (October 12, 2023)

    1. Updated README.md and updated the script.

TOP

Documentation

Overview

Package gettokenbyserviceaccount (gettokenbyserviceaccount.go) : This is a Golang library to retrieve access token from Service Account of Google without using Google's OAuth2 package.

# Install You can get this by $ go get -u github.com/tanaikech/go-gettokenbyserviceaccount

More information is https://github.com/tanaikech/go-gettokenbyserviceaccount

---------------------------------------------------------------

Package gettokenbyserviceaccount (gettokenbyserviceaccount.go) : This is a Golang library to retrieve access token from Service Account of Google without using Google's OAuth2 package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int64  `json:"expires_in"`
	TokenType   string `json:"token_type"`
	Start       int64  `json:"start_time"`
	End         int64  `json:"end_time"`
}

AccessToken :

func Do

func Do(privateKey, clientEmail, impersonateEmail, scopes string) (*AccessToken, error)

Do : Retrieve access token from Google Service Account

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL