skunkwerk
skunkwerk

Reputation: 3070

Adwords API getting customerId

I have an adwords user's Oauth credentials, and I'm able to successfully make an API call to download a report, but I had to hard-code in the customerId as I couldn't figure out how to get it.

The get() method of the CustomerService in the API should return the customer ID, but what are the parameters to pass to it?

I tried a GET request to this URL without any header params or body params, to which it returns:

 "<html><body>No service was found.</body></html>".

I also tried with an Authorization and developerToken defined in the headers, but got the same response.

I also tried the python library, and defined an instance of AdWordsClient like this, but it's client_customer_id variable ends up as None:

from googleads.adwords import AdWordsClient

oauth_client = GoogleRefreshTokenClient(environ.get('GOOGLE_CLIENT_ID'), environ.get('GOOGLE_CLIENT_SECRET'), oauth_record.refresh_token)
adwords_client = AdWordsClient(environ.get('ADWORDS_DEVELOPER_TOKEN'), oauth_client, user_agent='agent'))
customer_id = adwords_client.client_customer_id
print 'customer_id:' + str(customer_id)

Unlike this question, I don't care about adding the user to my MCC account.

Upvotes: 3

Views: 3878

Answers (2)

Nishant Gaurav
Nishant Gaurav

Reputation: 1

from googleads import oauth2, adwords

oauth_client = oauth2.GoogleRefreshTokenClient(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, refresh_token)
adwords_client = adwords.AdWordsClient(ADWORDS_DEVELOPER_TOKEN, oauth_client, user_agent='your_user_agent')
customers = adwords_client.GetService('CustomerService', version='v201809').getCustomers()
print(customers['customer_id'])

Upvotes: 0

skunkwerk
skunkwerk

Reputation: 3070

Here is how to get the customer ID with the googleads-python-lib library:

oauth_client = GoogleRefreshTokenClient(environ.get('GOOGLE_CLIENT_ID'), environ.get('GOOGLE_CLIENT_SECRET'), oauth_record.refresh_token)
adwords_client = AdWordsClient(environ.get('ADWORDS_DEVELOPER_TOKEN'),  oauth_client, user_agent='your_user_agent_here')
customer = adwords_client.GetService('CustomerService').get()
customer_id = customer['customerId']

Upvotes: 2

Related Questions