Getting error 401 using Google API documents to get/read it's data

Viewed 9665

I used this link to read/get data of the Google docs document.

NOTE: I changed document ID and API key for security reasons.

URL that I use

https://docs.googleapis.com/v1/documents/1oGPxB3BFf9XSAfe23lqY7OzYkn9_xvXaOQCGjBZEHmS4RSr21Y?key=AIzaSyDk5yXM8zFfg7KIgercswedLYD1bZO6zBue2tO0fk

Error that I got

{
  "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"
  }
}
2 Answers

{ "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" } }

Means that the method you are calling requires authorization and your call has not been authenticated.

If you check the doucmentation for document.get

You will see that this method requires authorization in order to access enter image description here

Methods that require authorization must be accessed using an authorization header.

API keys are used for accessing public data. If this is your own file and you will only ever be accessing that file then i sugest that you look into useing a service account service accounts can be preauthorized to allow you to access them without a user having to grant your application access.

If this is a file owned by a user of your application you will need to use OAuth2 and request access of the user.

Unfortunately, in the current stage, Google Docs API cannot be used by the API key. Even when is's the GET method, such error occurs. I think that this is the current specification. So how about the following workarounds?

1. Use access token.

Please use the access token retrieved by the OAuth2 and the service account. By this, Docs API can be used. I think that this is the recommended way by Google side.

2. Use Web Apps.

Also, you can achieve your goal using Web Apps created by Google Apps Script as an API. In this case, you can access to Web Apps without the access token. And also, you can also access to it using a unique key.

Note:

  • Docs API is growing now. So this might be resolved in the future update. But in the current stage, for example, you can request this to the issue tracker as the future request. Ref

References:

Related