Posts tagged with google-oauth

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?

I want to create a ads video reporting tool. So I used Google Ads API. For that I need to get customer ID. There are some programming examples. But I want to use an HTTP request endpoint. I cannot find any method for getting customer ID's with HTTP request. Is there any method for getting customer_id with HTTP request?

It seems the Google Ads API Client Library (PHP in my case) can automatically handle the access tokens by using a provided refresh token.

Does this mean that the client library will end up making additional calls in order to generate a new access token on every request?

If so, would it be better if I store the access token and pass it with each request and then track when it expires and handle generating a new one myself?

I'm using oAuth2.0 flow to work with Google Ads API. I ran in a problem today that when doing an API Request I'm getting the following 401 error:

UNAUTHENTICATED: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. 

On the headers of each google ads request I add

   {"Content-Type", "application/json"},    {"Accept", "application/json"},    {"User-Agent", "Mozilla/5.0 (Macintosh; ..."},    {"Authorization", "Bearer $ACCESS_TOKEN"},    {"developer-token", "$DEVELOPER_TOKEN"},    {"login-customer-id", "$LOGIN_CUSTOMER_ID"} 

I've already tried to generate a new access_token, since my automatically process of generating new access_tokens when the current one expired could be failing, but it wasn't the case. I managed to create a new valid access token and it continues to give the same error. The scope used when fist granted permissions to the app was "https://www.googleapis.com/auth/adwords".

Did anyone fall into the same problem?

My question is the same as described here: How to set request header in google ads api but I'm still facing the problem.
I'm trying to make an API call to google ads. I tried Rest API as well as google.ads.google_ads library. Both ways fail. I followed all Oath2 steps and got all ids, tokens, secrets, etc. The Rest API call is :

refresh_token = MY_REFRESH_TOKEN customer_id = 7868****** developer_token = MY_DEVELOPER_TOKEN access_token =  MY_ACCESS_TOKEN url = 'https://googleads.googleapis.com/v6/customers/{}/campaignBudgets:mutate'.format(customer_id) headers = {'developer-token':developer_token,"Authorization": "Bearer {}".format(access_token),'login-customer-id':'2693******',"Content-Type": "application/json"} response = requests.post(url,headers = headers) print(response.text) 

and this is the response:

{   "error": {     "code": 401,     "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",     "status": "UNAUTHENTICATED"   } } 

Why? I provided an access token.

I also tried the "google-ads" code I found in the documentation:
https://developers.google.com/google-ads/api/docs/reporting/example
I generated a googleads.ymal file, with the following content:

developer_token: MY_DEVELOPER_TOKEN user_agent: MY_COMPANY_NAME client-id: 7868****** login-customer-id: 2693****** client_id: MY_CLIENT_ID client_secret: MY_CLIENT_SECRET refresh_token: MY_REFRESH_TOKEN 

and this is what I get:

Error with message "User doesn't have permission to access customer. Note: If you're accessing a client customer, the manager's customer id must be set in the 'login-customer-id' header 

and again I don't understand why. My user was the one to generate all login details, and it has access to the manager account, which listed as "login-customer-id".

Can anyone shed some light?

Thanks