php - 执行 Google Sheets API php Quickstart 授权时出现 Http Err 500
问题描述
我一直在关注 Google Sheets API v4 以通过 PHP 在我的 Google Sheet 中执行特定任务。我遵循了API 快速入门,只是复制粘贴了代码并在我的代码中更改了工作表的凭据。当我运行代码时,页面未加载并出现此错误:
localhost 当前无法处理此请求。HTTP 错误 500。
我不知道为什么它没有加载。这几行可能有问题,但我不确定:
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
我的代码如下
<?php
require __DIR__ . '/vendor/autoload.php';
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('credentials.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} 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);
// Store the credentials to disk.
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);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path)
{
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
// 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]);
}
}
// [END sheets_quickstart]
我实际上不知道如何在我的本地主机中运行这个 api。
解决方案
使用以下命令运行示例:
php quickstart.php / or your php file name
第一次运行示例时,它会提示您授权访问:
在 Web 浏览器中浏览到提供的 URL。
如果您尚未登录 Google 帐户,系统会提示您登录。如果您登录了多个 Google 帐户,系统会要求您选择一个帐户用于授权。单击接受按钮。
复制您提供的代码,将其粘贴到命令行提示符中,然后按Enter。
推荐阅读
- javascript - 动态合并类似于 SQL JOIN 的数据:通过函数 INDEX 和 MATCH:google 电子表格
- typescript - 在定义中,仅当泛型类型是数组时,我才需要特定的方法签名
- python - 在 keras 中加载具有自定义损失的模型(缺少成员)
- elixir - 在这个 ExUnit 测试运行器中,为什么在这里调用 ExUnit.Server.modules_loaded()?
- c - 在简单的汇编 x86 中无法理解 %rax 的值
- reactjs - React Native (View) onLayout 不执行函数
- angular - 服务器端渲染 Angular 6.1
- sql - 如何在 SQL Server 中显示 2 个带有“分组依据”的查询作为一个查询
- django - 无法将 DB 的所有行打印到 Django 模板
- go - ZMQ 无法接收来自多个发布者的消息