The detail information and how to get this are https://github.com/tanaikech/goodls.
go-getfilelist was updated to v1.0.2
You can get this from https://github.com/tanaikech/go-getfilelist
Gists
This is a sample golang script for retrieving access token using Service Account of Google by Google’s OAuth2 package.
The script without using Google’s OAuth2 package is here.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
)
func serviceAccount(credentialFile string) (*oauth2.Token, error) {
b, err := ioutil.ReadFile(credentialFile)
if err != nil {
return nil, err
}
var c = struct {
Email string `json:"client_email"`
PrivateKey string `json:"private_key"`
}{}
json.Unmarshal(b, &c)
config := &jwt.Config{
Email: c.Email,
PrivateKey: []byte(c.PrivateKey),
Scopes: []string{
"https://www.googleapis.com/auth/drive.metadata.readonly",
},
TokenURL: google.JWTTokenURL,
}
token, err := config.TokenSource(oauth2.NoContext).Token()
if err != nil {
return nil, err
}
return token, nil
}
func main() {
token, err := serviceAccount("credentials.json") // Please set here
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(res)
}
Overview
This is a Golang library to retrieve access token from Service Account of Google without using Google’s OAuth2 package.
You can get this from https://github.com/tanaikech/go-gettokenbyserviceaccount
Gists
Updated on June 22, 2024
This is a sample script for retrieving the access token for Service Account using Google Apps Script. The flow for using this script is as follows.
Usage
- Create the Service Account and retrieve JSON file.
- Put Scopes,
private_key and client_email as an object.
- If you want to use impersonate email, please set the value of
impersonate_email.
- Run the script.
/**
* ### Description
* Get access token from service account.
* ref: https://tanaikech.github.io/2018/12/07/retrieving-access-token-for-service-account-using-google-apps-script/
*
* @param {Object} object Object including private_key, client_email, impersonate_email.
* @param {String} object.private_key
* @param {String} object.client_email
* @param {String} object.impersonate_email
* @param {Array} object.scopes
* @returns {String} Access token.
*/
function getAccessTokenFromServiceAccount_(object) {
const { private_key, client_email, impersonate_email = "", scopes = [] } = object;
const url = "https://www.googleapis.com/oauth2/v4/token";
const header = { alg: "RS256", typ: "JWT" };
const now = Math.floor(Date.now() / 1000);
const claim = { iss: client_email, scope: scopes.join(" "), aud: url, exp: (now + 3600).toString(), iat: now.toString() };
if (impersonate_email != "") {
claim.sub = impersonate_email;
}
const signature = Utilities.base64Encode(JSON.stringify(header)) + "." + Utilities.base64Encode(JSON.stringify(claim));
const jwt = signature + "." + Utilities.base64Encode(Utilities.computeRsaSha256Signature(signature, private_key));
const params = { payload: { assertion: jwt, grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer" } };
const res = UrlFetchApp.fetch(url, params);
const { access_token } = JSON.parse(res.getContentText());
return access_token;
}
// Please run this script.
function main() {
const object = {
private_key: "-----BEGIN PRIVATE KEY-----\n###-----END PRIVATE KEY-----\n",
client_email: "###",
// impersonate_email: "###", // If you want to use an impersonate email, please set this.
scopes: ["###", "###",,,],
};
const accessToken = getAccessTokenFromServiceAccount_(object);
console.log(accessToken);
}
Sample script
When you can obtain the access token by the above script, you can also use the following sample script. The following sample script retrieves the file list of the drive of the service account with Method: files.list of Drive API v3.
You can see the detail information here https://github.com/tanaikech/DownloadLargeFilesByUrl
ggsrun was updated to v.1.6.0
- v1.6.0 (November 30, 2018)
- Although at ggsrun, files can be searched by filename and file ID, searching files using search query and regex couldn’t be done. From version 1.6.0, files got to be able to be searched using the search query and regex.
$ ggsrun sf -q "### search query ###" -f "### fields ###" -r "### regex ###"
- Some modifications.
You can check ggsrun at https://github.com/tanaikech/ggsrun.
The detail information and how to get this are https://github.com/tanaikech/goodls.
The detail information and how to get this are https://github.com/tanaikech/goodls.
Gists
This sample script is for using the string values to []googleapi.Field for Golang. The property of fields can often be used to the Google APIs. When such APIs are used by the Go library, there are the cases that fields parameter is required to be used. For example, at the quickstart of Drive API for golang, the value is directly put to Fields() like r, err := srv.Files.List().PageSize(10).Fields("nextPageToken, files(id, name)").Do(). For this situation, when the string value is put to Fields() as follows,