PHP Google Sheets API-权限被拒绝

PHP Google Sheets API-权限被拒绝,php,google-api,google-api-php-client,permission-denied,google-sheets-api,Php,Google Api,Google Api Php Client,Permission Denied,Google Sheets Api,嗨,我刚刚开始学习PHP,我正在尝试使用GoogleSheetsAPI 我认为我遵循了他们的指示,我可以很好地调用和编辑工作表数据,但只能公开访问 当我将访问状态更改为private或Group时,我会遇到下面的消息 PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "The caller does

嗨,我刚刚开始学习PHP,我正在尝试使用GoogleSheetsAPI

我认为我遵循了他们的指示,我可以很好地调用和编辑工作表数据,但只能公开访问

当我将访问状态更改为private或Group时,我会遇到下面的消息

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"
  }
}
我会把我的密码放在你能看到的地方

<?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);

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


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

如果您通过身份验证的用户无权访问该文件,则您将无法写入该文件。您必须始终具有访问私人用户数据的权限。通过将它设置为公共,你是在说任何人都可以给它写信。但如果将其设置为私有或组,则只有组的所有者或成员才能向其写信。谢谢您的评论。但问题是,我已经使用我的电子邮件和帐户在控制台中创建了工作表文档和凭证。因此,我的帐户是所有权限和OAuth的所有者。有关权限的更多信息,是否需要检查?如果您使用与文件所有者相同的帐户授权您的php应用程序(登录“是”“访问我的数据”),我会检查文件id。如果您的电子邮件设置为所有者,您能否转到文件->共享->高级并检查共享设置?另外,我假设您的访问令牌没有过期。添加API的服务帐户后(blah@blahblah.iam.gserviceaccount.com)在我的工作表共享设置中,它运行良好..:(感谢您帮助我发现问题。我不再需要密钥“client_secret.json”。这是正确的方法吗?