<?php
require_once __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('America/Los_Angeles');
define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', 'token.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SHEET_ID', '1Ygd6cEvi0tcGa3GF11hUIZfomh0Ms9r1mZN-MfcqruE');
define('SCOPES', implode(' ', array(
Google_Service_Sheets::SPREADSHEETS)
));
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$refreshToken = $client->getRefreshToken();
$client->refreshToken($refreshToken);
$newAccessToken = $client->getAccessToken();
$newAccessToken['refresh_token'] = $refreshToken;
file_put_contents($credentialsPath, json_encode($newAccessToken));
}
return $client;
}
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = SHEET_ID;
$range = 'Sales 3!A5:B';
$values = array(
array('Appended', 'Row')
);
$body = new Google_Service_Sheets_ValueRange(array(
'values' => $values
));
$params = array(
'valueInputOption' => "RAW"
);
$result = $service->spreadsheets_values->append($spreadsheetId, $range,
$body, $params);
我正在审核权限错误:
致命错误:未捕获Google_Service_Exception:{“error”:{“code”:403,“message”:“请求的身份验证范围不足。”,“错误”:[{“message”:“请求已经认证范围不足。“,”“域”:“全局”,“原因”:“禁止”},“状态”:“PERMISSION_DENIED”}}在C:\ xampp \ htdocs \ dashboard-redmine.org \ vendor \ google \ apiclient \ src \ Google \ Http \ REST.php:118堆栈跟踪:#0 C:\ xampp \ htdocs \ dashboard-redmine.org \ vendor \ google \ apiclient \ src \ Google \ Http \ REST.php(94) :Google_Http_REST :: decodeHttpResponse(对象(GuzzleHttp \ Psr7 \ Response),对象(GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#1 C:\ xampp \ htdocs \ dashboard-redmine.org \ vendor \ google \ apiclient \ src \ Google \ Task \ Runner.php(176):Google_Http_REST :: doExecute(Object(GuzzleHttp \ Client),Object(GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#2 C:\ xampp \ htdocs \ dashboard-redmine.org \ vendor \ google \ apiclient \ src \ Google \ Http \ REST.php(58):Google_Task_Runner-&gt;运行()#3 C:\ xampp \ htdocs \ dashboa在C:\ xampp \ H第118行的tdocs \ dashboard-redmine.org \ vendor \ google \ apiclient \ src \ Google \ Http \ REST.php
但是当我使用get方法时,它正常工作 并且在面板中它也正常工作:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append?apix=true
答案 0 :(得分:0)
为了更好地指导您,请检查您已完成的流程,但尚未完成。
首先要做的是,将工作表api启用到developer console。然后,如果您认为自己错过了某些内容,可以在documentation中查看授权的范围和流程。
最后,尝试撤消先前从/Users/yourUserName/.credentials/sheets.googleapis.com-projectName/*
保存的用户,然后重新执行以获取新凭据。