Google Ads API with Google Apps Script no access token received
I am trying to use Google Ads on Google Apps Script using this guide. I created client id and client secret on Google Console's API & Services.
Not too sure if this configuration is correct but the account is linked to Google Apps Script as I have pagespeed insights as well and there are some requests on the dashboard. I added https://www.googleapis.com/auth/drive
as the scope. Again not too sure if I should add Google Ads to the scope. Lastly, got my refresh token from Google Auth playground. When I run the script above I got the following error:
Error: No access token received: { "error": "invalid_client", "error_description": "Unauthorized" } authenticate_ @ test.gs:120 withRefreshToken @ test.gs:144 initializeOAuthClient @ test.gs:28
Honestly not too sure what I am doing wrong here so any help would be very much appreciated. Thank you.
Edit Codes:
//From Google Console API & Services var CLIENT_ID = '"MY_CLIENT_ID'; var CLIENT_SECRET = 'MY_CLIENT_SECRET'; //From Google Authplayground var REFRESH_TOKEN = 'REFRESH_TOKEN'; // Enter scopes which should match scopes in File > Project properties // For this project, e.g.: https://www.googleapis.com/auth/drive var SCOPES = "https://www.googleapis.com/auth/adwords"; // Script ID taken from 'File > Project Properties' var SCRIPT_ID = 'MY_SCRIPT_ID'; var authUrlFetch; // Call this function just once, to initialize the OAuth client. function initializeOAuthClient() { if (typeof OAuth2 === 'undefined') { var libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library'; throw Error('OAuth2 library not found. Please take a copy of the OAuth2 ' + 'library from ' + libUrl + ' and append to the bottom of this script.'); } var tokenUrl = 'https://accounts.google.com/o/oauth2/token'; authUrlFetch = OAuth2.withRefreshToken(tokenUrl, CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN, SCOPES); } /** * Execute a remote function. * @param {string} remoteFunctionName The name of the function to execute. * @param {Object[]} functionParams An array of JSON objects to pass to the * remote function. * @return {?Object} The return value from the function. */ function executeRemoteFunction(remoteFunctionName, functionParams) { var apiParams = { 'function': remoteFunctionName, 'parameters': functionParams }; var httpOptions = { method: 'POST', headers: { 'Content-Type': 'application/json' }, payload: JSON.stringify(apiParams) }; var url = 'https://script.googleapis.com/v1/scripts/' + SCRIPT_ID + ':run'; var response = authUrlFetch.fetch(url, httpOptions); var data = JSON.parse(response.getContentText()); // Retrieve the value that has been returned from the execution. if (data.error) { throw Error('There was an error: ' + response.getContentText()); } return data.response.result; } // Paste in OAuth2 library here, from: // https://developers.google.com/google-ads/scripts/docs/examples/oauth20-library
I have pasted the oauth2.0 library under the codes above.
Edit 2
I fixed the part of function initializeOAuthClient
. It now shows execution complete, but when I try to run function executeRemoteFunction
I am getting TypeError: Cannot read property 'fetch' of undefined
. I am guessing I have to input remoteFunctionName
and functionParams
but where do I find them?