“PERMISSION_DENIED” on Google Spreadsheet REST API

2020-02-12 18:39发布

问题:

I try to read data from google sheet via http. I use v4 google spreadsheet api:

https://sheets.googleapis.com/v4/spreadsheets/1MZJEOzkzy5iJ9HPy2GU-Ky3vdaIzGGDQH8BMt4n3OGI/values/Sheet1!A1:D5

I get:

{
  "error": {
    "code": 403,
    "message": "The request cannot be identified with a client project. Please pass a valid API key with the request.",
    "status": "PERMISSION_DENIED"
  }
}

Sheet is published online and everyone can edit it. It works on api v3 too.

回答1:

First, make sure you have a permission to call this spreadsheet and you enable the Sheets API in your developer console. You can also try to change the privacy settings of this spreadsheet to public and checked if you can access it now. This 403 error is usually caused by incorrect or missed some configuration in Authorizing Requests. If you authorizing requests with OAuth 2.0, make sure you use the proper scope with it.

For more information, you can check this thread on how to debug your Google OAuth 2.0 token when you get HTTP 401s or 403s



回答2:

You have to append the string "key=yourAPIkey" as query on your URI. The API key is generated in the developer's console



回答3:

If you update application access scope you have to re-authenticate the application and update credentials.json. In google-sheet quick start guide, they have started the tutorial with only read scope. If you want to write or update google-sheet later you will have to change the scope there and update the credentials.json again Look carefully when you first authenticate it only giving permission to read. But if update scope and try to re-authenticate it will ask for permission to read and manage sheet permission.



回答4:

In order to update the scopes, you need to remove the token file saved in TOKENS_DIRECTORY_PATH, execute the code again and log in. Then Oauth2 will give you the new updated tokens.