Google Oauth2: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'

Dean

I have the following code running on my localhost to try and authenticate with Google Oauth2. After authenticating, it appears to work for a while. Then after a certain amount of time I'm getting this classic error:

Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}'

I've looked at the previous times this issue was posted to StackOverflow, but the suggested solutions didn't seem to work for me.

I believe the access token is updated correctly (see code below). I also believe the server clock is synchronized correctly. What am I doing wrong?


    //$token retrieved from database
    $token = {"access_token":"xxx.xxx_xxxx-xxx","token_type":"Bearer","expires_in":3600,"refresh_token":"1\/xXxXxxxxxXXXxx","created":145490000}

    $google = new Google_Client();
    $google->setAccessType('offline');
    $google->setClientId($client_id);
    $google->setClientSecret($client_secret);
    $google->setRedirectUri($client_redirect_uri);
    $google->setAccessToken($token);
    if($google->isAccessTokenExpired()) {
      $google->refreshToken($token);
    }
    $token = $google->getAccessToken();
    $google->setAccessToken($token);

    //Some Database code to save the new $token
    ...

    $result = new \Google_Service_YouTube($google);
DaImTo

There are two posibple causes for Invalid_Grant

  1. server clock not sync with NTP. (solution fix the time)
  2. The refresh token has expired. (solution request authentication again)

You stated that your code works for a while then stops working. This makes me think your problem is nr 2.

Reasons why a refresh token will stop working:

  1. The user in question has revoked your authentication.
  2. If a refresh token hasn't been used for 6 months will automatically expire.
  3. Refresh token is based upon the project (Client Id) and the user. If the user authenticates your application you get a refresh token. If they do it again you get another refresh token. The first one will still work as will the second. You can have up to 26 live refresh tokens once the user authenticates your code for the 27 th time you will loose the first refresh token.

To solve your problem you need to try and figure out which of the 3 problems above has caused your refresh token to expire. I cant really help you much with that its going to require some detective work on your part.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Circumstances of the "invalid_grant" error when refreshing an access token?

分類Dev

Google Oauth2:OAuth2トークンの更新中にエラーが発生しました。メッセージ: '{"error": "invalid_grant"}'

分類Dev

Error refreshing the OAuth2 token @ Google_CalendarServices (php)

分類Dev

Google oAuth2アクセストークン: "invalid_grant"

分類Dev

OAUth2 ASP.NET invalid_grant

分類Dev

Google API OAuth2、サービスアカウント、「エラー」:「invalid_grant」

分類Dev

{"error": "invalid_grant"} Google Directory APIPHPクライアントSDKのエラー

分類Dev

BoxAPIのoAuth2を試行するときのinvalid_grantエラー

分類Dev

'OAuth2トークンの更新中にエラーが発生しました、メッセージ:' {"error": "invalid_grant"} ''ライブサイトでは動作しますが、ローカル環境では機能します

分類Dev

oauth2client.client.HttpAccessTokenRefreshError:invalid_grant:無効なJWT

分類Dev

Spring Boot: error="invalid_grant", error_description="Bad credentials"

分類Dev

GoogleからoAuthトークンを取得しようとしているinvalid_grant

分類Dev

Google_Auth_Exception:invalid_grant

分類Dev

Googleサービスアカウントの例では、「OAuth2トークンの更新中にエラーが発生しました{「エラー」:「invalid_grant」}」が返されます

分類Dev

GoogleApiサービスアカウント "error": "invalid_grant"、 "error_description": "無効なJWT署名。"

分類Dev

Stripe API; {'error': 'invalid_grant'、 'error_description': 'この認証コードはすでに使用されています

分類Dev

Google oauth2 returns no id_token

分類Dev

google oauth2 get token javascript post request

分類Dev

Implementing Oauth2 login, Fatal error: Class 'Google_Service' not found

分類Dev

OAuth2 access token

分類Dev

Google OAuth2 api

分類Dev

Docusign PHP getting "invalid_grant: unsupported_grant_type" when trying to get token (JWT auth)

分類Dev

Google API PHP offline access, "invalid_grant: Code was already redeemed"

分類Dev

Google Cloud Storage HttpAccessTokenRefreshError:invalid_grant:不正なリクエスト

分類Dev

'invalid_grant:無効なJWT:Google翻訳Api

分類Dev

How do you use Google API getRequestHeaders() to get an OAuth2 access token?

分類Dev

Google drive OAuth2 without browser

分類Dev

Google CalendarAPIがinvalid_grantと不正なリクエストを返します

分類Dev

Mirror API invalid_grant

Related 関連記事

  1. 1

    Circumstances of the "invalid_grant" error when refreshing an access token?

  2. 2

    Google Oauth2:OAuth2トークンの更新中にエラーが発生しました。メッセージ: '{"error": "invalid_grant"}'

  3. 3

    Error refreshing the OAuth2 token @ Google_CalendarServices (php)

  4. 4

    Google oAuth2アクセストークン: "invalid_grant"

  5. 5

    OAUth2 ASP.NET invalid_grant

  6. 6

    Google API OAuth2、サービスアカウント、「エラー」:「invalid_grant」

  7. 7

    {"error": "invalid_grant"} Google Directory APIPHPクライアントSDKのエラー

  8. 8

    BoxAPIのoAuth2を試行するときのinvalid_grantエラー

  9. 9

    'OAuth2トークンの更新中にエラーが発生しました、メッセージ:' {"error": "invalid_grant"} ''ライブサイトでは動作しますが、ローカル環境では機能します

  10. 10

    oauth2client.client.HttpAccessTokenRefreshError:invalid_grant:無効なJWT

  11. 11

    Spring Boot: error="invalid_grant", error_description="Bad credentials"

  12. 12

    GoogleからoAuthトークンを取得しようとしているinvalid_grant

  13. 13

    Google_Auth_Exception:invalid_grant

  14. 14

    Googleサービスアカウントの例では、「OAuth2トークンの更新中にエラーが発生しました{「エラー」:「invalid_grant」}」が返されます

  15. 15

    GoogleApiサービスアカウント "error": "invalid_grant"、 "error_description": "無効なJWT署名。"

  16. 16

    Stripe API; {'error': 'invalid_grant'、 'error_description': 'この認証コードはすでに使用されています

  17. 17

    Google oauth2 returns no id_token

  18. 18

    google oauth2 get token javascript post request

  19. 19

    Implementing Oauth2 login, Fatal error: Class 'Google_Service' not found

  20. 20

    OAuth2 access token

  21. 21

    Google OAuth2 api

  22. 22

    Docusign PHP getting "invalid_grant: unsupported_grant_type" when trying to get token (JWT auth)

  23. 23

    Google API PHP offline access, "invalid_grant: Code was already redeemed"

  24. 24

    Google Cloud Storage HttpAccessTokenRefreshError:invalid_grant:不正なリクエスト

  25. 25

    'invalid_grant:無効なJWT:Google翻訳Api

  26. 26

    How do you use Google API getRequestHeaders() to get an OAuth2 access token?

  27. 27

    Google drive OAuth2 without browser

  28. 28

    Google CalendarAPIがinvalid_grantと不正なリクエストを返します

  29. 29

    Mirror API invalid_grant

ホットタグ

アーカイブ