Skip to content

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

License

Notifications You must be signed in to change notification settings

tanaikech/go-gettokenbyserviceaccount

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages