gpt4 book ai didi

PHP Google Sheets API - 权限被拒绝

转载 作者:行者123 更新时间:2023-12-04 17:57:17 39 4
gpt4 key购买 nike

您好,我刚开始学习 PHP,我正在尝试使用 Google 表格 API。

我以为我按照他们的指示进行操作,我可以很好地调用和编辑工作表数据,但仅限于公共(public)访问。

当我将访问状态更改为私有(private)或群组时,我会遇到以下消息。

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

代码中有什么需要确认的吗?还是谷歌 API 控制台?

如有任何信息,我们将不胜感激!提前致谢!

最佳答案

文件->共享->高级并添加服务账号

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

关于PHP Google Sheets API - 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330695/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com