How to get access to a private spreadsheet from the API?

半世苍凉 提交于 2019-11-30 20:34:52

问题


Hello I need to get access to a spreadsheet from google spreadsheet API. I have enabled Google Sheets API and added API key, but this doesn't work!!! https://sheets.googleapis.com/v4/spreadsheets/[my_spreadsheet_id]/values/A1?key=[my_api_key]

The result is: 403 - The caller does not have permission

But if I provide public access for this spreadsheet ---> it works!!!

The result is: 200 - [correct requested data .....]

The my question is: How to get access to a private spreadsheet from the API?

I need to read and write orders data there from my website using php's functions like file_get_contents()


回答1:


Basically, you need to get authorization as mentioned in the documentation.

Whenever your application requests private user data, it must send an OAuth 2.0 token along with the request. Your application first sends a client ID and, possibly, a client secret to obtain a token. You can generate OAuth 2.0 credentials for web applications, service accounts, or installed applications.

Further information from OAuth 2.0 documentation, when accessing a Google API using OAuth 2.0, all applications follow these steps:

  1. Obtain OAuth 2.0 credentials from the Google API Console.
  2. Obtain an access token from the Google Authorization Server.

    Before your application can access private data using a Google API, it must obtain an access token that grants access to that API.

  3. Send the access token to an API.
  4. Refresh the access token, if necessary.

Lastly, to help you with the implementation using PHP, you may also add Authentication and authorization to your list of references. Hope that helps!



来源:https://stackoverflow.com/questions/39747381/how-to-get-access-to-a-private-spreadsheet-from-the-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!