【问题标题】:Error 500 at google spreadsheets_values get method谷歌电子表格值获取方法中的错误 500
【发布时间】:2019-09-01 14:41:42
【问题描述】:

我阅读了很多帖子,但在调用 google sheet spreadsheets_values->get 方法时找不到解决错误 500 的原因或方法。

这是我的代码,很标准

header('Access-Control-Allow-Origin: https://script.google.com');
header('Access-Control-Allow-Origin: https://www.google.com');

require __DIR__ . '/vendor/autoload.php';

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('SPD Web Services');
    $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);


$spreadsheetId = '1mTrQp8yzioqLpr9m_2or1wLkVzvZpWYz65RJyIqxxxx';
$range = 'PABS';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (!empty($values)) {
    foreach ($values as $row) {
        $abslist .= "<li>{$row[0]}</li>";
    }
}

exit;    

我尝试在不同的行添加出口,发现错误发生在

$response = $service->spreadsheets_values->get($spreadsheetId, $range);

我的代码有错误吗?是否有任何调试控制台可以让我找到原因?

最好的问候,

开尔文。

【问题讨论】:

    标签: php google-sheets-api


    【解决方案1】:

    抱歉,我犯了一个重大错误,我没有从 Google 控制台启用 API 和服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多