How to avoid 'Failed to retrieve access token: { “error” : “invalid_grant” }' in offline GAE cron tasks?

前端 未结 1 1071
野的像风
野的像风 2020-12-29 16:15

This post is a followup to How to make \'access_type=offline\' / server-only OAuth2 operations on GAE/Python. The http = credentials.authorize(httplib2.Http())

相关标签:
1条回答
  • 2020-12-29 16:52

    An invalid_grant is returned when the refresh token can't be used to get a new access token from the current user. This is happening to you because the stored Credentials object has a null refresh token, i.e.

    >>> credentials.refresh_token is None
    True
    

    As mentioned in the NOTE in How to make 'access_type=offline' / server-only OAuth2 operations on GAE/Python?:

    If a user has already authorized your client ID, the subsequent times you perform OAuth for these users they will not see the OAuth dialog and you won't be given a refresh token.

    You need to make sure your Credentials are stored with a valid refresh token and the easiest way to do this, as mentioned in your last question as well as in all 3 questions you linked to is to use approval_prompt=force when creating your OAuth2WebServerFlow or OAuth2Decorator object (whichever you are using).

    0 讨论(0)
提交回复
热议问题