Accessing google Ads API using a service account

2,235 views
Skip to first unread message

Siddharth Mehta

unread,
Sep 2, 2023, 8:54:00 AM9/2/23
to Google Ads API and AdWords API Forum

I've been trying to authenticate to google ads API using my service account using the following code in python

import pandas as pd
from google.ads.googleads.client import GoogleAdsClient

def main():
    try:
        # Initialize the Google Ads API client with the service account key file
        client = GoogleAdsClient.load_from_storage("content-api-key.json")

        # Define a customer ID (your Google Ads account ID)
        customer_id = 'MY_CUSTOMER_ID'

        # Define a query to fetch campaign data (modify as needed)
        query = (
            f"SELECT campaign.id, campaign.name, metrics.impressions, metrics.clicks "
            f"FROM campaign "
            f"WHERE segments.date DURING LAST_7_DAYS "
            f"AND campaign.status = 'ENABLED'"
        )

        # Create a Google Ads service client
        ga_service = client.service.google_ads

        # Execute the query and fetch campaign data
        response = ga_service.search(
            customer_id=customer_id,
            query=query,
        )

        # Process and print the results (modify as needed)
        for row in response:
            print(
                f"Campaign ID: {row.campaign.id.value}, "
                f"Name: {row.campaign.name.value}, "
                f"Impressions: {row.metrics.impressions.value}, "
                f"Clicks: {row.metrics.clicks.value}"
            )

    except Exception as ex:
        print(f"An error occurred: {ex}")

if __name__ == "__main__":
    main()

i have got the service account key generated as a json file. I am getting the following error:

An error occurred: The client library configuration is missing the required "use_proto_plus" key. Please set this option to either "True" or "False".

Now i am confused as service account doesnt need any configuration file so where do i add this "use_proto_plus" key and solve this problem. Any help would be appreciated.

Google Ads API Forum Advisor

unread,
Sep 4, 2023, 6:13:45 AM9/4/23
to siddharth...@gmail.com, adwor...@googlegroups.com

Hi,

 

Thank you for raising your concern to the Google Ads API team.

 

I understand you encountered an issue while configuring your client library. Kindly refer to this guide, additionally for more information on the needed configuration fields, check here. Since you are using a service account, you need to add a private key JSON and an impersonated email to configuration, more information on how to do this can be found on this documentation.

 

Please note that when reading in the configuration, the library first looks for credentials for the OAuth2 desktop or web app flows, and will use them if found. So if you want to use a Service Account authentication, make sure credentials for the other flows are not present in your configuration.

 

Let me know if you have any questions.

 
This message is in relation to case "ref:_00D1U1174p._5004Q2oAR0I:ref"

Thanks,
 
Google Logo Google Ads API Team


Reply all
Reply to author
Forward
0 new messages