Google token endpoint returning unsupported grant type

129 Views Asked by At

I'm trying to implement Google Oauth to get information on users who sign in with Google such as their name and profile photo.

I found this react library which seems like it can get me a code when using the flow: 'auth_code' param.

Additionally I've found these Google docs which outline how to exchange a code for an ID token which will have user information.

I can successfully retrieve a code using the react component as shown in this slightly redacted log message from my browser:

{
    "code": "4/0AWtgzh4VK3QT85egP0m9Rsau_gND-FPDxpM-cPB7ocxTfNkZSCzFSbE86<redacted suffix>",
    "scope": "email profile openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
    "authuser": "0",
    "prompt": "consent"
}

I then send this code to my backend which fires off the following request to the token endpoint as found in the Discovery document, mentioned in the OpenID Google docs:

    try {
      const resp = await axios.post("https://oauth2.googleapis.com/token", {
        code: googleJWT,
        client_id: getGoogleClientId(),
        client_secret: googleSecret,
        redirect_uri: "http://localhost:3000",
        grant_type: "authorization_code"
      }, {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      })
      console.log("################## posted", resp)
    } catch (e) {
      console.log("################## could not post")
      console.log(e)
    }

However, this results in an error being thrown:

      error: 'unsupported_grant_type',
      error_description: 'Invalid grant_type: '

even though a grant type of authorization_code is reported as supported by the Discovery document.

For completeness, here's the redacted axios config that gets printed with the above error message:

 config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function: httpAdapter],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: [Function: validateStatus],
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/x-www-form-urlencoded',
      'User-Agent': 'axios/0.26.1',
      'Content-Length': 298
    },
    method: 'post',
    url: 'https://oauth2.googleapis.com/token',
    data: '{"code":"4/0AWtgzh4VK3QT85egP0m9Rsau_gND-FPDxpM-cPB7ocxTfNkZSCzFSbE86<redacted>","client_id":"221274282648-q9nrm8j61s5cba218qot926vnloua2hb.apps.googleusercontent.com","client_secret":"<redacted>","redirect_uri":"http://localhost:3000","grant_type":"authorization_code"}'
  },

Why is google complaining that the provided grant type isn't supported and how can I swap this code for an ID token?

0

There are 0 best solutions below