web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Google API token with Service Account

(0) ShareShare
ReportReport
Posted on by 15

Intent: try to upload files to Google Storage Folder

Auth type: google Service Account with Private Key

i have to run the following script to obtain token from google.

This script auto-generates a Google OAuth token from a Service Account key,

* and stores that token in accessToken variable in Postman.

*

* Prior to invoking it, please paste the contents of the key JSON

* into serviceAccountKey variable in a Postman environment.

*

* Then, paste the script into the "Pre-request Script" section

* of a Postman request or collection.

*

* The script will cache and reuse the token until it's within

* a margin of expiration defined in EXPIRES_MARGIN.

*

* Thanks to:

* paw.cloud/.../google-service-apis

* developers.google.com/.../OAuth2ServiceAccount

* gist.github.com/.../b57985b0649d3407a7aa9de1bd327990

* github.com/.../1607

*/

const ENV_SERVICE_ACCOUNT_KEY = 'serviceAccountKey';

const ENV_JS_RSA_SIGN = 'jsrsasign';

const ENV_TOKEN_EXPIRES_AT = 'tokenExpiresAt';

const ENV_ACCESS_TOKEN = 'accessToken';

const JS_RSA_SIGN_SRC = 'kjur.github.io/.../jsrsasign-latest-all-min.js';

const GOOGLE_OAUTH = 'www.googleapis.com/.../token';

// add/remove your own scopes as needed

const SCOPES = [

   'https://www.googleapis.com/auth/cloud-platform'

];

const EXPIRES_MARGIN = 300; // seconds before expiration

const getEnv = name =>

   pm.environment.get(name);

const setEnv = (name, value) =>

   pm.environment.set(name, value);

const getJWS = callback => {

   // workaround for compatibility with jsrsasign

   const navigator = {};

   const window = {};

   let jsrsasign = getEnv(ENV_JS_RSA_SIGN);

   if (jsrsasign) {

       eval(jsrsasign);

       return callback(null, KJUR.jws.JWS);

   }

   pm.sendRequest(JS_RSA_SIGN_SRC, (err, res) => {

       if (err) return callback(err);

       jsrsasign = res.text();

       setEnv(ENV_JS_RSA_SIGN, jsrsasign);

       eval(jsrsasign);

       callback(null, KJUR.jws.JWS);

   });

};

const getJwt = ({ client_email, private_key }, iat, callback) => {

   getJWS((err, JWS) => {

       if (err) return callback(err);

       const header = {

           typ: 'JWT',

           alg: 'RS256',

       };

       const exp = iat + 3600;

       const payload = {

           aud: GOOGLE_OAUTH,

           iss: client_email,

           scope: SCOPES.join(' '),

           iat,

           exp,

       };

       const jwt = JWS.sign(null, header, payload, private_key);

       callback(null, jwt, exp);

   });

};

const getToken = (serviceAccountKey, callback) => {

   const now = Math.floor(Date.now() / 1000);

   if (now + EXPIRES_MARGIN < getEnv(ENV_TOKEN_EXPIRES_AT)) {

       return callback();

   }

   getJwt(serviceAccountKey, now, (err, jwt, exp) => {

       if (err) return callback(err);

       const req = {

           url: GOOGLE_OAUTH,

           method: 'POST',

           header: {

               'Content-Type': 'application/x-www-form-urlencoded',

           },

           body: {

             mode: 'urlencoded',

             urlencoded: [{

                 key: 'grant_type',

                 value: 'urn:ietf:params:oauth:grant-type:jwt-bearer',

             },{

                 key: 'assertion',

                 value: jwt,

             }],

           },

       };

       pm.sendRequest(req, (err, res) => {

           if (err) return callback(err);

           const accessToken = res.json().access_token;

           setEnv(ENV_ACCESS_TOKEN, accessToken);

           setEnv(ENV_TOKEN_EXPIRES_AT, exp);

           callback();

       });

   });

};

const getServiceAccountKey = callback => {

   try {

       const keyMaterial = getEnv(ENV_SERVICE_ACCOUNT_KEY);

       const serviceAccountKey = JSON.parse(keyMaterial);

       callback(null, serviceAccountKey);

   } catch (err) {

       callback(err);

   }

};

getServiceAccountKey((err, serviceAccountKey) => {

   if (err) throw err;

   getToken(serviceAccountKey, err => {

       if (err) throw err;

   });

});

can anyone help me out with the logic and how to query google.

  • Suggested answer
    YUN ZHU Profile Picture
    103,085 Super User 2026 Season 2 on at

    Hi, hope the following helps.

    https://developers.google.com/drive/api/guides/search-files

    Thanks.

    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 715 Super User 2026 Season 2

#2
AndrewThomas81 Profile Picture

AndrewThomas81 390 Super User 2026 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 386 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans