PHP Google Sheets API-权限被拒绝 [英] PHP Google Sheets API - Permission Denied

查看:66
本文介绍了PHP Google Sheets API-权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始研究PHP,并且正在尝试使用Google Sheets API.

Hi I've just started to study PHP and I'm attempting to use the Google sheets API.

我认为我可以按照他们的指示进行操作,并且可以很好地调用和编辑工作表数据,但只能使用公开访问权限.

I thought I followed their instructions and I could call and edit sheet data well but only Public Access.

当我将访问状态更改为私人"或组"时,我在下面遇到消息.

When I change access status as private or Group, I encounter message below.

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "errors": [
      {
        "message": "The caller does not have permission",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

然后,我会在代码中提供您希望提供的帮助.

And I will put my codes what you would see to help.

<?php
require_once __DIR__ . '/vendor/autoload.php';

define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', __DIR__ . '/service-account-credentials.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
        Google_Service_Sheets::SPREADSHEETS)
));

putenv('GOOGLE_APPLICATION_CREDENTIALS='.CREDENTIALS_PATH);

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Drive::DRIVE);

$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);

$tokenArray = $client->fetchAccessTokenWithAssertion();
$accessToken = $tokenArray["access_token"];

$service = new Google_Service_Sheets($client);

$spreadsheetId = "1qe7QwyYxWuIAYT7UUZ2s3KdDZrAHJ02uPH4VPrr-pMk";

$range = "Sheet1";
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
    print "No data found.\n";
}
print_r($values);

代码中是否需要确认?或Google API控制台?

Is there anything to confirm in the code? or google API console?

任何信息将不胜感激!预先感谢!

Any information would be greatly appreciated! thanks in advance!

推荐答案

文件->共享->高级并添加服务帐户

File->Share->Advanced and add the service account

例如: your@email.iam.gserviceaccount.com

这篇关于PHP Google Sheets API-权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆