Reputation: 196
I'm new to AdWords, and am trying to set up my first API request per the instructions shown here using Google's AdWords API Python Client: https://developers.google.com/adwords/api/docs/guides/first-api-call I've followed the instructions as best I can and can only get the API call to return: google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', '{\n "error": "invalid_grant",\n "error_description": "Bad Request"\n}')
Per the instructions, I've tried the following sequence:
1. Set up an AdWords Manager account and requested my developer token
2. Received notice that my dev token is now pending
3. Created a Test Manager account using an email address that is different from the one that I used to request the developer token
4. Created Test Campaigns under the Test Manager account
5. Installed the Python client library per the provided instructions
6. In the Google Developer Console associated with the email address used to request my developer token, I have set up the OAuth Client ID and received my client_ID and client_secret
7. Used these credentials into the generate_refresh_token.py file and run the script to receive the URL to generate the refresh token.
8. Navigated to the provided URL and signed in using the TEST MANAGER account and received a refresh token
9. In the googleads.yaml file (which is at my root directory of my hard drive) I've added the developer token from the AdWords Manager account (not the test account), the client_id and client_secret generated in step 6, the refresh token generated in step 8, and the client customer id from the TEST MANAGER account.
10. Created a Python file with the example API script from here: https://github.com/googleads/googleads-python-lib/blob/master/examples/adwords/v201809/basic_operations/get_campaigns.py
Here is the example code:
from googleads import adwords
PAGE_SIZE = 100
def main(client):
# Initialize appropriate service.
campaign_service = client.GetService('CampaignService', version='v201809')
# Construct selector and get all campaigns.
offset = 0
selector = {
'fields': ['Id', 'Name', 'Status'],
'paging': {
'startIndex': str(offset),
'numberResults': str(PAGE_SIZE)
}
}
more_pages = True
while more_pages:
page = campaign_service.get(selector)
# Display results.
if 'entries' in page:
for campaign in page['entries']:
print ('Campaign with id "%s", name "%s", and status "%s" was '
'found.' % (campaign['id'], campaign['name'],
campaign['status']))
else:
print ('No campaigns were found.')
offset += PAGE_SIZE
selector['paging']['startIndex'] = str(offset)
more_pages = offset < int(page['totalNumEntries'])
if __name__ == '__main__':
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client)
Expected Output: JSON response with all campaigns
Actual Output: google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', '{\n "error": "invalid_grant",\n "error_description": "Bad Request"\n}')
Upvotes: 2
Views: 1875
Reputation: 196
**** EDIT: I RESOLVED MY ISSUE ****
The script for generate_refresh_token.py is written in Python 2, and I was attempting to run it in Python 3. Line 101 of the script uses the raw_input() method, which has been changed in Python 3 to input(). This was causing an error in the script execution that was not prompting me to enter the access code that I was being given after navigating to the URL in step 8 (I was being given an access token, NOT the refresh token as I had previously thought).
After changing the script to input() and running it in the Python 3 compiler I was prompted to provide the access code which returned the refresh code on the command line. I hope this helps someone else struggling with the Python API client.
Upvotes: 2