Service Account

Executing Google Apps Script with Service Account

Gists Abstract One day, you might have a situation where it is required to run Google Apps Script using the service account. Unfortunately, in the current stage, Google Apps Script cannot be directly run with the service account because of the current specification. So, this report introduces a workaround for executing Google Apps Script using the service account. Introduction When you want to execute Google Apps Script from outside of Google, as the basic approach, it can be achieved by Google Apps Script API.

Using Until Expiration Time of Access Token Retrieved By googleapis for Python

Gists When Google APIs are used with googleapis for Python, the client is obtained as follows. creds = service_account.Credentials.from_service_account_file(service_account_credential_file, scopes=scopes) service = build("drive", "v3", credentials=creds) In this case, when the script is run, the access token is retrieved every time. But, the expiration time of the retrieved access token is 1 hour. Here, there might be the case that you want to use the access token until the expiration time. It is considered that effectively using the access token will lead to SDGs.

Retrieving Access Token from Service Account using oauth2client and google-auth with Python

Gists This is a sample script for retrieving the access token from the service account using oauth2client and google-auth with Python. Sample script 1 Use oauth2client. from oauth2client.service_account import ServiceAccountCredentials SERVICE_ACCOUNT_FILE = "credentials.json" SCOPES = ["https://www.googleapis.com/auth/drive"] creds = ServiceAccountCredentials.from_json_keyfile_name(SERVICE_ACCOUNT_FILE, scopes=SCOPES) res = creds.get_access_token() access_token = res.access_token print(access_token) Sample script 2 Use google-auth. In the current stage, this method might be general. from google.oauth2 import service_account import google.auth.transport.requests SERVICE_ACCOUNT_FILE = "credentials.

Using OnEdit trigger on Google Spreadsheet Created by Service Account

Gists In the current stage, by the current specification, Google Apps Script cannot be directly run on Google Spreadsheet created by Service Account. But, there is a case in that we want to use the OnEdit trigger on the Spreadsheet that the service account is the owner. In this post, I would like to introduce the method for achieving this. Recently, I published “Using OnEdit Trigger to Google Spreadsheet by Hiding Google Apps Script from Other Users”.

Using Google API Client Library (gapi) for JavaScript with Service Account

Gists This is a sample script for using Google API Client Library (gapi) for JavaScript with the service account. Unfortunately, in the current stage, gapi cannot directly use the service account. So, in this case, it is required to implement the script for retrieving the access token from the service account. In this report, I would like to introduce the method for using gapi with the service account using a Javascript library.

Javascript library - GetAccessTokenFromServiceAccount_js

Overview This is a Javascript library to retrieve the access token from the Google Service Account. Ref Description I have already posted the sample script for retrieving the access token from the Google Service Account. Ref But, when I use this script, I thought that when this was published as the Javascript library, it will be useful. So I created this. Install <script src="getaccesstokengromserviceaccount_js.min.js"></script> Or, using jsdelivr cdn <script src="https://cdn.jsdelivr.net/gh/tanaikech/GetAccessTokenFromServiceAccount_js@master/getaccesstokengromserviceaccount_js.min.js"></script> You can see the detail of this at https://github.

Retrieving Access Token using Service Account for PHP without using googleapis

Gists This is a sample PHP script to retrieve the access token from Service Account of Google without using googleapis. Sample script <?php $private_key = "-----BEGIN PRIVATE KEY-----\n###-----END PRIVATE KEY-----\n"; // private_key of JSON file retrieved by creating Service Account $client_email = "###"; // client_email of JSON file retrieved by creating Service Account $scopes = ["https://www.googleapis.com/auth/drive.readonly"]; // Sample scope $url = "https://www.googleapis.com/oauth2/v4/token"; $header = array("alg" => "RS256", "typ" => "JWT"); $now = floor(time()); $claim = array( "iss" => $client_email, "sub" => $client_email, "scope" => implode(" ", $scopes), "aud" => $url, "exp" => (string)($now + 3600), "iat" => (string)$now, ); $signature = base64_encode(json_encode($header, JSON_UNESCAPED_SLASHES)) .

Safe-Uploading for Google Drive by HTML in External Server using Google Apps Script

Overview This is a report for safe-uploading files to Google Drive by HTML put in the external server using Google Apps Script. Description When you want to make the user upload a file to your own Google Drive using the HTML put in the external server of Google side, when the file size is smaller than 50 MB, this can be achieved without using the access token. Ref (When the HTML is put in the internal server of Google side, you can also use google.

Retrieving Access Token for Service Account using Javascript

Gists This is a sample script for retrieving the access token for Service Account using Javascript. The flow for using this script is as follows. At first, please create the Service Account and retrieve JSON file. Put Scopes, private_key and client_email to the script. Run the script. Sample script In this script, 2 libraries of jsencrypt and crypto-js are used. <script src="https://cdnjs.cloudflare.com/ajax/libs/jsencrypt/3.0.0-rc.1/jsencrypt.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script> <script> async function sample() { const private_key = "###"; // private_key of JSON file retrieved by creating Service Account const client_email = "###"; // client_email of JSON file retrieved by creating Service Account const scopes = ["https://www.

Retrieving Access Token using Service Account for Node.js without using googleapis

Gists This is a sample Node.js script to retrieve access token from Service Account of Google without using googleapis. const cryptor = require('crypto'); const request = require('request'); const privateKey = "###"; // private_key of JSON file retrieved by creating Service Account const clientEmail = "###"; // client_email of JSON file retrieved by creating Service Account const scopes = ["https://www.googleapis.com/auth/drive.readonly"]; // Sample scope const url = "https://www.googleapis.com/oauth2/v4/token"; const header = { alg: "RS256", typ: "JWT", }; const now = Math.

Retrieving Access Token using Service Account by Google's OAuth2 package for Golang

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.

Retrieving Access Token for Service Account using Google Apps Script

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.