Google Spreadsheet API问题

时间:2017-02-08 11:45:36

标签: php api google-sheets

我收到以下错误:

Uncaught exception 'Google_Service_Exception' with message 
{ "error": { "code": 403, "message": "The request is missing a valid API key.", 
"errors": [ { "message": "The request is missing a valid API key.", "domain": "global", 
"reason": "forbidden" } ], 
"status": "PERMISSION_DENIED" } 
}

我的代码是:

$client = new Google_Client();
$client->setAuthConfig('google-api-php-client-2.1.1/client_s‌​ecret.json');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setRedirectUri($redirect_uri);
$spreadsheetId = '1Ybwc_wNhvc6tVQIRY-33qwkY0WoGj09uIQ3YQqa9fvo';
$range = 'sample!A2:E'; $response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
    print "No data found.\n";
} else {
    print "Name, Major:\n";
    foreach ($values as $row) {
          printf("%s, %s\n", $row[0], $row[4]);
    }
}

任何人都可以帮助/指导我找到问题的原因吗?

2 个答案:

答案 0 :(得分:0)

如果这是针对php客户端库的,请参阅 Using OAuth 2.0 for Web Server Applications有关启用API和获取代码凭据的信息。从该页面注释:

  1. 要在Web应用程序中使用OAuth 2.0,请先在API控制台中为项目创建Web应用程序凭据。
  2. 所有使用OAuth 2.0的Web应用程序都必须具有标识OAuth 2.0服务器应用程序的凭据。具有这些凭据的应用程序可以访问为项目启用的API
  3. 上述链接页面上的信息将指导您获取有效的API密钥。

答案 1 :(得分:0)

如果稍后更新应用程序范围,则必须重新进行身份验证并更新credentials.json。

相关问题