0

我很快需要将数据逐行添加到现有的约 1000 条记录的电子表格中。我想我想通过制作一个小的 PHP 页面来让我的生活更轻松,该页面将显示一行的数据并为我提供一个表单来将我想要的数据添加到该行。电子表格位于云端硬盘中,因此可以将我带到云端硬盘 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 ?>
4

1 回答 1

0

原来在开发者控制台中只启用了 Drive API,而不是 SDK。谢谢!

于 2014-07-30T10:33:21.720 回答