Google试算表中的错误500值获取方法 [英] Error 500 at google spreadsheets_values get method

查看:59
本文介绍了Google试算表中的错误500值获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通读了很多文章,但是在调用google sheetsheets_values-> get方法时找不到解决错误500的原因或方法.

I read through a lot of posts but cannot find the reason or method to troubleshoot the error 500 when calling google sheet spreadsheets_values->get method.

这是我的代码,非常标准

Here is my code, very standard

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;    

我试图将出口添加到另一行,并发现错误发生在

I tried to add the exit at the different line and find the error was occurred at

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

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

Is there any error to my code? Is there any debug console allowing me to find the reason?

最诚挚的问候,

开尔文.

推荐答案

对于我的重要错误,很抱歉,我没有从Google控制台启用API和服务.

Sorry for my important mistake, I didn't enable the APIs and Services from Google console.

这篇关于Google试算表中的错误500值获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆