问题
I'll be needing to add data to an existing ~1000-record spreadsheet row by row soon. I thought I want to make my life easier by making a small PHP page that would show me a row's data and provide me with a form to add the data I want to that row. the spreadsheet is in Drive so that leads me to the Drive API! :)
I've downloaded the Google API client manager and got to work with the OAuth 2.0 example (that shortens a URL). That all worked great, but now I'm trying to fetch some metadata off the spreadsheet that I need. No matter which kind of call I use regarding drive, I always get the following error:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/drive/v2/files: (403) Insufficient Permission'
Any idea what might cause this? For reference, here's my code:
<?php
session_start();
set_include_path("google-api-php-client-master/src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
$client_id = 'xxx';
$client_secret = 'xxx';
$redirect_uri = 'http://localhost/coding/';
//SETTING UP THE CLIENT
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope('https://www.googleapis.com/auth/drive');
$client->addScope('https://spreadsheets.google.com/feeds');
$service = new Google_Service_Drive($client);
//QUICK LOGOUT MECHANISM
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
//HANDLE OAUTH
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
//TALK TO DRIVE
if ($client->getAccessToken()) {
//THIS GOES WRONG :)
$service->files->get('xxx');
}
if (isset($authUrl)): ?>
<a class='login' href='<?php echo $authUrl; ?>'>Connect Me!</a>
<?php else: ?>
<a class='logout' href='?logout'>Logout</a>
<?php endif ?>
回答1:
Turns out only the Drive API was enabled in the developer console, not the SDK. Thanks!
来源:https://stackoverflow.com/questions/25032570/insufficient-permissions-when-trying-to-get-google-spreadsheet-meta-info