简体   繁体   中英

Google Sheet API php curl get request

I am using google oauth v2 HTTP request to obtain the access toke which is working fine, I'm getting the token, but when using the token to access a spreadsheet I'm getting a 403 error.

This is the request code I am using.

$get = 'https://sheets.googleapis.com/v4/spreadsheets/1bHKgQLgqB4PfjRUVPkYk5_L7aQOtZGOkhJbr_RFxQQY?access_token='.$access_token;

$ch = curl_init();  

curl_setopt($ch,CURLOPT_URL,$get);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false); 
$output = curl_exec($ch);
curl_close($ch);

var_dump($output);

And this is the return with the header

HTTP/1.1 403 Forbidden
Vary: X-Origin
Vary: Referer
Content-Type: application/json; charset=UTF-8
Date: Tue, 15 Nov 2016 13:13:09 GMT
Server: ESF
Cache-Control: private
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Transfer-Encoding: chunked

{
  "error": {
  "code": 403,
  "message": "The caller does not have permission",
  "status": "PERMISSION_DENIED"
  }
}

I've tired to make the sheet public but that makes no difference to the error. The error is not displaying on the API console either.

These are the scopes I am using:

{
   "iss":"dev-537@mortgagetest-149109.iam.gserviceaccount.com",
   "scope":"https://www.googleapis.com/auth/drive    
    https://www.googleapis.com/auth/drive.readonly 
    https://www.googleapis.com/auth/spreadsheets 
    https://www.googleapis.com/auth/spreadsheets.readonly",
   "aud":"https://accounts.google.com/o/oauth2/token",
   "exp":'.($time+3600).',
   "iat":'.$time.'
}

What am I doing wrong?

Solved the problem

The sheet I was trying to access needed sharing with my client ID (email address) Right click on the sheet in the google drive and share with the email address.

Posting this for anyone with the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM