Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()

like image 682
Gms Brothers Avatar asked Sep 28 '16 12:09

Gms Brothers


People also ask

How do I access a spreadsheet created by Google API?

To access the data stored in Google Sheets, you will need to create a service account and get a set of OAuth2 credentials from the Google API Console. Access the Google APIs Console while logged into your Google account. Create a new project and give it a name. Click on ENABLE APIS AND SERVICES .


1 Answers

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!

like image 138
Teyam Avatar answered Oct 11 '22 22:10

Teyam