Google ads data using api

653 views
Skip to first unread message

Xolane Gift

unread,
Apr 6, 2023, 3:46:44 AM4/6/23
to Google Ads API and AdWords API Forum
Hello, I am trying to extract google ads data. Please send the code and the error below. Any advise for for me will be appreciated.

import proto
import json

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException


def main(client, customer_id):
    ga_service = client.get_service("GoogleAdsService")

    query = """
        SELECT
          campaign.id,
          campaign.name
        FROM campaign
        ORDER BY campaign.id"""

    # Issues a search request using streaming.
    stream = ga_service.search_stream( query=query,customer_id=customer_id)
   
    data = list()
    for batch in stream:
        for row in batch.results:
            newrow = proto.Message.to_json(
                row,
                preserving_proto_field_name=True,
                use_integers_for_enums=False,
                including_default_value_fields=False
            )
            data.append(json.loads(newrow))

    return data

client = GoogleAdsClient.load_from_dict(credentials)
df=main(client,'9521974869')
print(df)

These is the error that I am getting:
raceback (most recent call last):
  File "C:\Users\gift\Desktop\Googel_Ads.py", line 42, in <module>
    client = GoogleAdsClient.load_from_dict(credentials)
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\ads\googleads\client.py", line 268, in load_from_dict
    kwargs = cls._get_client_kwargs(config_data)
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\ads\googleads\client.py", line 182, in _get_client_kwargs
    "credentials": oauth2.get_credentials(config_data),
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\ads\googleads\oauth2.py", line 114, in get_credentials
    return get_installed_app_credentials(
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\ads\googleads\oauth2.py", line 48, in initialize_credentials_wrapper
    credentials.refresh(Request())
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\oauth2\credentials.py", line 335, in refresh
    ) = reauth.refresh_grant(
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\oauth2\reauth.py", line 349, in refresh_grant
    _client._handle_error_response(response_data, retryable_error)
  File "C:\Users\gift\AppData\Local\Programs\Python\Python311\Lib\site-packages\google\oauth2\_client.py", line 73, in _handle_error_response
    raise exceptions.RefreshError(
google.auth.exceptions.RefreshError: ('unauthorized_client: Unauthorized', {'error': 'unauthorized_client', 'error_description': 'Unauthorized'})

Google Ads API Forum Advisor

unread,
Apr 10, 2023, 1:53:32 AM4/10/23
to xman...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

Regarding your concern, it appears that you are experiencing issues while extracting data using the Python client library. Also, it appears that this error is the unauthorized_client error. Having said that, since you are using the Python client Library,  I would recommend checking and referring to these Installation and Configuration guides that need to be followed to avoid issues on your end. Also, our Authorization guide for more details about the authorization process in the Google Ads API.

Links included in this email:
Regards,
Google Logo Google Ads API Team


ref:_00D1U1174p._5004Q2kSXK8:ref

Xolane Gift

unread,
Apr 10, 2023, 5:24:51 AM4/10/23
to Google Ads API Forum Advisor, adwor...@googlegroups.com
Thank you for your response. 
Reply all
Reply to author
Forward
0 new messages