问题描述
我刚刚开始研究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-权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!