Posts tagged with google-cloud-platform

I'm using the Google Ads API to retrieve information from Google Ads campaigns. Initially, I manually generated a new refresh token by following specific steps to obtain an authorization code using a designated Google ID. Subsequently, I sent a CURL request with that code to generate a refresh token. Now, I aim to automate the entire process to convert the script, responsible for fetching Google Ads campaign data, into a full-time scheduler by automating the generation and renewal of the refresh token. Unfortunately, I'm encountering difficulties in achieving this. Is there a specific code or method available to accomplish the aforementioned task?

I would like to obtain the process and code for the aforementioned issue.

For a week now I have been crawling the web but haven't find any legit documentation to connect the python librairie for Ads API with Service Account.

I want to create a micro-service, server-to-server, so anything else apart service account as authentication is off-table.

On GCP, I was using oAuth2 but I had to manually refresh the tokens with https://developers.google.com/oauthplayground/ which isn't a viable solution.

What I have done so far :

  1. I created a service account
  2. generated a key to it in JSON and downloaded it.
  3. Add all possible access to my service-account permissions, such as :
  • Owner
  • Security Reviewer
  • Service Account Admin
  • Service Account Key Admin
  • Service Account Token Creator
  • Service Account User

4 - Created my script to authenticate :

from google.ads.googleads.client import GoogleAdsClient credentials = {                 "developer_token":  "xXxxxXxxxXxxXXXXxXxxX",                 "use_proto_plus":  true,                 "json_key_file_path":  "./path_to_json.json" ,                 "impersonated_email":  "gcp_and_ads_admin_email",                 "login_customer_id":  "XXXXXXXXXX"             } client = GoogleAdsClient.load_from_dict(credentials) 

When I execute my script, this error follows :

google.auth.exceptions.RefreshError: ('unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.', {'error': 'unauthorized_client', 'error_description': 'Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.'})

Does anyone know how I can find a tutorial or help me with this please ?

I'm using OAuth2 flow to get access to a Google Ads Account. My application runs in a flask server, so I can get the authorization code from the OAuth2 flow using the the requestvariable in the flask context. First, I get the permission screen url, that redirects to "/google_ads/authorized" flask route.

auth_url = 'https://accounts.google.com/o/oauth2/auth' client_id = os.environ['CLIENT_ID'] client_secret = os.environ['CLIENT_SECRET'] redirect_uri = os.environ['REDIRECT_URI'] scope = 'https://www.googleapis.com/auth/adwords' 

In the redirected flask route I'm making a request to exchange the authorization code by the access token and refresh token.

token_url = 'https://accounts.google.com/o/oauth2/token' payload = {  "code": request.args['code'],  "client_id": client_id,  "client_secret": client_secret,  "redirect_uri": redirect_uri,  "grant_type": "authorization_code" } req = requests.post(url=token_url, data=json.dumps(payload) res = req.json() credentials = {  'access_token': res['access_token'],   'refresh_token': res['refresh_token'],   'client_id': client_id,  'client_secret': client_secret,   'expiry': '2023-06-12T18:01:59',   'scopes': ['https://www.googleapis.com/auth/adwords'] } 

Finally, I use the credentials dict to build a Credentials object, that is passed with the developer token as an argument to instantiate the GoogleAdsClient,

from google.oauth2.credentials import Credentials from google.ads.googleads.client import GoogleAdsClient developer_token = os.environ['DEVELOPER_TOKEN'] credentials=Credentials.from_authorized_user_info(creds) client = GoogleAdsClient(credentials=credentials, developer_token=developer_token) customer_service = client.get_service("CustomerService") customer_service.list_accessible_customers() 

When I try to make a request and list the available customers in the account, using customer_service.list_accessible_customers(), I got the following error:

{  "error": {    "code": 403,    "message": "The caller does not have permission",    "status": "PERMISSION_DENIED",    "details": [      {       "@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",       "errors": [      {        "errorCode": {           "authorizationError": "DEVELOPER_TOKEN_PROHIBITED"        },        "message": "Developer token is not allowed with project '685646918301'."          }        ],        "requestId": "hYMHcD_FbdI0vwYeKRrAbw"       }     ]   } } 

This method is the same method that I'm using to authenticate Google Analytics API requests. I already tried using the flow lib and I got the same error.

Error while generating refresh token in google console

I am trying to generate the refresh token using desktop flow.i have successfully created client id and client secret. I am getting the consent prompt but when I allow next page it’s throwing error 127.0.0.1 site can’t be reached . I am following the documentation and using generate_refresh_token.py to generate the token . As per the doc it should work for both desktop and web app

https://developers.google.com/google-ads/api/docs/client-libs/python/oauth-web