OAuth2.0 token strange behaviour (Invalid Credentials 401)

后端 未结 13 1651
無奈伤痛
無奈伤痛 2020-12-05 00:13

Usually, Google OAuth2.0 mechanism is working great.

  1. The user confirms permission to access Google account with selected scopes.
  2. The refresh token is
13条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 00:34

    Per the Google API docs on errors & error codes:

    https://developers.google.com/drive/handle-errors#401_invalid_credentials

    401: Invalid Credentials
    
    Invalid authorization header. The access token you're using is either expired or invalid.
    
    error: {
      errors: [
       {
      "domain": "global",
      "reason": "authError",
      "message": "Invalid Credentials",
      "locationType": "header",
      "location": "Authorization",
      }
      ],
      "code": 401,
      "message": "Invalid Credentials"
      }
    }
    

    This matches your version of the error exactly, and so is very probably what Google thinks is wrong with your request.

    But, as you well know, Google API requests can return errors that are distinctly unhelpful to actually diagnosing the problem. I have gotten "Invalid Credentials" errors for a number of reasons. It is almost always really because I have made some sort of change that I thought would not matter, but really does.

    My first thought (shot in the dark here) would be to go to the Google API console:

    https://code.google.com/apis/console

    Googles auth token verifier ( https://www.googleapis.com/oauth2/v1/tokeninfo ) can return a valid response, but maybe the client secret or client id will have been changed.

    Even tiny changes in the response body can also cause this error.

    I don't know how you are making requests, whether by REST calls or a client lib, but I use the ruby lib which allows a command line interface to making API calls. I have found this & the OAuth2 Playground very helpful in diagnosing Google API calls.

    Just an FYI: I have only gotten 2 errors from the Google API: "Invalid Credentials" and "Insufficient Permissions". The latter has almost always had to do with bad scopes. The former is just about everything else.

    I would also say that if you have only experienced 2 errors in 6 months, you are lucky!

提交回复
热议问题