php - Google API 快速入门
问题描述
我遇到了 Google API 快速入门的问题。我无法验证令牌,因为它说令牌格式无效。最奇怪的是几个月前我能够让它在另一个项目上运行,但现在我什么都做不了。
我的 quickstart.php 看起来像:
<?php
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);
// Prints the names and majors of students in a sample spreadsheet:
// https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
$spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms';
$range = 'Class Data!A2:E';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (empty($values)) {
print "No data found.\n";
} else {
print "Name, Major:\n";
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s\n", $row[0], $row[4]);
}
}
当我尝试输入令牌时,它返回以下错误
Fatal error: Uncaught InvalidArgumentException: Invalid token format in ..\vendor\google\apiclient\src\Google\Client.php:469
Stack trace:
#0 ..\quickstart.php(45): Google_Client->setAccessToken()
#1 ..\quickstart.php(63): getClient()
#2 {main}
thrown in ..\vendor\google\apiclient\src\Google\Client.php on line 469
我尝试$authCode
手动附加令牌,但没有成功。
解决方案
所以这确实是我的错误,我们的前置引擎“稍微”改变了链接,因此无法验证。由于我已更改为随机域并复制了令牌,因此一切正常。
推荐阅读
- android - 显示来自 Firebase 实时数据库的数据
- loops - 带计数器的递归过程?
- angular-cli - 如何更改 vis 时间线语言?
- java - .jsp 从 TOMCAT 6.0 迁移到 7.0 后显示为纯文本
- javascript - 如何将外部 javascript 文件添加到反应组件中
- mysql - 如何在mysql服务器上显示所有数据库的qps等统计信息
- spring-mvc - 使用 JSTL TAGS 将 Struts2 标签转换为 SpringMVC
- c# - WPF 应用程序(Core 或 Framework.net)是否支持 SVG,如果不支持,何时支持?
- node.js - 使用预签名 URL 和 AWS Lambda 函数(使用 Angular 8、Node.js 和 Netlify)将文件上传到 AWS S3 存储桶
- search - LinkedIn 抓取多个页面