我一步一步地执行的所有工作都遵循Google表格API建设

时间:2016-10-14 12:42:30

标签: php google-spreadsheet-api

我第一次尝试使用Google表格API来管理我的Google云端硬盘上的一些电子表格,并在该文档的“完整示例”部分中提供指南:https://developers.google.com/api-client-library/php/auth/web-app

虽然我在每个步骤中都采用了正确的方法,但是当PHP运行命令时,我总是收到错误:“HTTP ERROR 500”:

$response = $service->spreadsheets_values->get($spreadsheetId, $range);

我的项目目录结构是:

/public_html/api
--- vendor
--- client_secrets.json
--- composer.json
--- composer.lock
--- composer.phar
--- index.php
--- oauth2callback.php

我从此链接获得供应商目录:https://github.com/google/google-api-php-client/releases(google-api-php-client-2.0.3_PHP54.zip)

这是test.waytech.vn/api/index.php中的代码内容:

<?php
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
  $drive_service = new Google_Service_Drive($client);
  $files_list = $drive_service->files->listFiles(array())->getItems();
  echo json_encode($files_list);
} else {
  $redirect_uri = 'http://test.waytech.vn/api/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

这是test.waytech.vn/api/oauth2callback.php中的代码内容:

<?php
require_once __DIR__.'/vendor/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://test.waytech.vn/api/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = 'http://test.waytech.vn/api';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

我没有睡2个晚上来解决这个问题,但现在还找不到任何解决方案。

0 个答案:

没有答案