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'})