"权限不足"在尝试获取Google Spreadsheet元信息时

时间:2014-07-30 08:54:58

标签: php google-sheets google-drive-api google-client

我需要尽快将数据添加到现有的~1000条记录电子表格中。我想我想通过制作一个小的PHP页面来让我的生活变得更轻松,该页面会向我显示一行的数据,并为我提供一个表单来添加我想要的那一行数据。电子表格位于云端硬盘中,以便引导我访问Drive API! :)

我已经下载了Google API客户端管理器,并开始使用OAuth 2.0示例(缩短了URL)。这一切都很有效,但现在我正在尝试从我需要的电子表格中获取一些元数据。无论我在驱动器上使用哪种调用,我总是会收到以下错误:

  

致命错误:未捕获的异常'Google_Service_Exception',显示消息'错误调用POST https://www.googleapis.com/drive/v2/files :( 403)权限不足'

知道可能导致这种情况的原因吗?作为参考,这是我的代码:

<?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 个答案:

答案 0 :(得分:0)

原来只有开发人员控制台启用了Drive API,而不是SDK。谢谢!