Search code examples
node.jsoauth-2.0node-requestgoogle-my-business-api

Configuring request to Google My Business API to avoid Unauthenticated error


I keep getting an unauthenticated error back when submitting my request to the Google My Business API in my Node.js app. The response:

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

For what it's worth, I'm using the request-promise client to make the request. My function is below. I just received the access token, so I'm fairly certain that it's good, and I can see it through the err.options.Authorization log. I know the location ID doesn't exist yet, but I don't think that's what the error is telling me.

const request = require('request-promise');
...
function checkLocation (loc) {
  return request({
    method: 'GET',
    uri: `https://mybusiness.googleapis.com/v4/accounts/${ACCOUNT_ID}/locations/${loc._id}`,
    Authorization: `OAuth ${ACCESS_TOKEN}`
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (err) {
    console.error(err.error, err.options.Authorization);
  });
}

Is my request formatted incorrectly or am I not sending all I need to be?

Update: I left out possibly crucial information that this is through the one time authorization process for server-side apps like outlined here: https://developers.google.com/identity/protocols/OAuth2. My trouble is wrapped up in the sentence on that page, "After an application obtains an access token, it sends the token to a Google API in an HTTP authorization header."


Solution

  • Turned out my header wasn't formatted correctly. The Authorization key should be in a headers object:

    {
      method: 'GET',
      uri: `https://mybusiness.googleapis.com/v4/accounts/${ACCOUNT_ID}/locations/${loc._id}`,
      headers: {
    
        Authorization: `OAuth ${ACCESS_TOKEN}`
    
      }
    }