Reasons for the 'invalid_grant' error response

792 views
Skip to first unread message

Alex Kashirin

unread,
Jan 18, 2018, 10:07:27 AM1/18/18
to AdWords API Forum
Hello,
The following code is use to work with the adwords_client:

########################
import googleads   
    
client_id = ' '
client_secret = ' '
refresh_token = ' '
company = ' '
client_customer_id = ' '

# or with:
#  oauth2._GOOGLE_OAUTH2_ENDPOINT = 'https://accounts.google.com/o/oauth2/auth'
oauth2_client = oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token)
adwords_client = adwords.AdWordsClient(None, oauth2_client, company, client_customer_id=client_customer_id)

report_downloader = adwords_client.GetReportDownloader(version='v201710')
########################

The googleads's oauth2_client is doing a token refresh as necessary and the first request is the refresh that has the response code of invalid_grant.

What can be the reasons for the 'invalid_grant' error response ?
client_id, client_secret and refresh_token should be the correct ones, as give by client_secret_***.apps.googleusercontent.com.json

Thanks,
Kashirin Alex

Alex Kashirin

unread,
Jan 18, 2018, 10:32:07 AM1/18/18
to AdWords API Forum
Small fix, the oauth2 and adwords are modules of googleads. (ok, to try it)

########################
import googleads

client_id = ' '
client_secret = ' '
refresh_token = ' '
company = ' '
client_customer_id = ' '

# or with:
#  googleads.oauth2._TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
#  googleads.oauth2._GOOGLE_OAUTH2_ENDPOINT = 'https://accounts.google.com/o/oauth2/auth'
oauth2_client = googleads.oauth2.GoogleRefreshTokenClient(client_id, client_secret, refresh_token)
adwords_client = googleads.adwords.AdWordsClient(None, oauth2_client, company, client_customer_id=client_customer_id)

report_downloader = adwords_client.GetReportDownloader(version='v201710')
########################

Milind Sankeshware (AdWords API Team)

unread,
Jan 18, 2018, 3:21:56 PM1/18/18
to AdWords API Forum
Hi Alex,

The "invalid_grant" error usually indicates that the access or refresh token being passed in your request is incorrect or invalid. 

To verify that your credentials are correct, you could also try the CURL request provided below and see if you are able to generate the access tokens? If the credentials are correct, you should get an access token for the below request.
curl https://www.googleapis.com/oauth2/v4/token \
-d refresh_token=your-refresh_token \
-d client_id=your-client-id \
-d client_secret=your-client-secret \
-d grant_type=refresh_token

If your credentials are incorrect and still facing an issues then could you generate a new refresh token with the help of this guide and retry your request? Let me know if the issue persists.

Thanks,
Milind, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages