0
votes

I have a Google Ads script running to change campaign budgets, but implementation of the same script into Bing Ads is more difficult for me. I'm having problems with the code to connect Google Sheets with Bing Ads Script. I got clientId, clientSecret and refresh token to authorize Google service in Bing, but am struggling with the code to allow the script read my Google Sheets file.

I attached some code responsible for connecting Google Sheets file to Bing Script. It should allow it to read it's content and later change it to whatever values I provided in that file.

const credentials = {
      accessToken: '', // not sure if i needed it if I got refresh token
      clientId: 'HIDDEN',
      clientSecret: 'HIDDEN',
      refreshToken: 'HIDDEN'
  };

  function main() {

var SPREADSHEET_URL = 'HIDDEN';

var GoogleApis;
  (function (GoogleApis) {
      GoogleApis.readSheetsService = credentials => readService("https://sheets.googleapis.com/$discovery/rest?version=v4", credentials);
       
      // Creation logic based on https://developers.google.com/discovery/v1/using#usage-simple
      function readService(SPREADSHEET_URL, credentials) {
          const content = UrlFetchApp.fetch(SPREADSHEET_URL).getContentText();
          const discovery = JSON.parse(content);
          const accessToken = getAccessToken(credentials);
          const standardParameters = discovery.parameters;
      }

  function getAccessToken(credentials) {
      if (credentials.accessToken) {
          return credentials.accessToken;
      }
      const 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' } });
      const responseCode = tokenResponse.getResponseCode();
      const responseText = tokenResponse.getContentText();
      if (responseCode >= 200 && responseCode <= 299) {
          const accessToken = JSON.parse(responseText)['access_token'];
          return accessToken;
      }
      throw new Error(responseText);
   })(GoogleApis || (GoogleApis = {}));

it throws syntax error on the last line of the code: })(GoogleApis || (GoogleApis = {}));

but i think there is more than that.

1

1 Answers

1
votes

Please try the var GoogleApis declaration outside main() as this example shows: https://docs.microsoft.com/en-us/advertising/scripts/examples/calling-google-services

I hope this helps.