简体   繁体   中英

Google Spreadsheet API issue

I am getting the following error:

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" } 
}

My code is:

$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]);
    }
}

Can anyone help/guide me in find the cause of the issue?

If this is for the php client library, see Using OAuth 2.0 for Web Server Applications for information regarding enabling the API and getting Credentials for your code. From that page note:

  1. To use OAuth 2.0 in a web application, first create web application credentials for your project in the API Console.
  2. All web applications that use OAuth 2.0 must have credentials that identify the application to the OAuth 2.0 server. Applications that have these credentials can access the APIs that you enabled for your project

The information on the above linked page will guide you in getting a valid API key.

如果以后更新应用程序范围,则必须重新进行身份验证和更新凭据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM