9

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

Gms Brothers
  • 115
  • 1
  • 5
  • I think you answered your own question. You cannot access a private resource (_cause its private_) – RiggsFolly Sep 28 '16 at 12:24
  • Ok, but how to provide access to my spreadsheet from the PHP script, but not for everyone, who have the public link? (without public link) – Gms Brothers Sep 28 '16 at 12:36
  • I guess you have to share it with a specific account as writable, then use that account in the login credentials. – RiggsFolly Sep 28 '16 at 12:39
  • Do I need to use "service account" in google console for this purpose? – Gms Brothers Sep 28 '16 at 12:49
  • So, if we cannot use api_key to access private Sheet file. And with access_token we can only access private file for limit of duration, and then we have to re-authorize with OA2.0 . Anyone have solution to access Sheet file permanently? – ytdm Jun 17 '22 at 04:36
  • @Mr_Thorynque Dead link – BingLi224 Dec 19 '22 at 04:40

1 Answers1

7

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!

Teyam
  • 7,686
  • 3
  • 15
  • 22