MicrosoftDocs / Advertising

Public sync with MicrosoftDocs/Advertising-pr
Creative Commons Attribution 4.0 International
11 stars 31 forks source link

Automatically pulling Access Token #142

Closed parasoul20 closed 4 months ago

parasoul20 commented 4 months ago

In advertising/scripts/examples/calling-google-services.md

I'm having troubles with

function getAccessToken(credentials) { if (credentials.accessToken) { return credentials.accessToken; } var tokenResponse = UrlFetchApp.fetch('https://www.googleapis.com/oauth2/v4/token', { method: 'post', contentType: 'application/x-www-form-urlencoded', muteHttpExceptions: true, payload: { client_id: credentials.clientId, client_secret: credentials.clientSecret, refresh_token: credentials.refreshToken, grant_type: 'refresh_token' } }); var responseCode = tokenResponse.getResponseCode(); var responseText = tokenResponse.getContentText(); if (responseCode >= 200 && responseCode <= 299) { var accessToken = JSON.parse(responseText)['access_token']; return accessToken; } throw responseText; } })(GoogleApis || (GoogleApis = {}));

If I don't hard code access_tokenin the credentials, the function can't pull the 'access_token' automatically as I get the error 'The OAuth client was not found.'

Am I missing something?

I'm looking to automatically pull the access token from the Google Playground without having to copy it manually every time.