无法使用php

时间:2016-01-11 11:37:34

标签: php google-drive-api google-drive-realtime-api

我正在使用谷歌驱动API与PHP。基本上我创建了一个身份验证凭据并停留在我要列出谷歌驱动器文件的位置。这是我尝试的代码。

<?php
require_once realpath(dirname(__FILE__) . '/gac/src/Google/autoload.php');

$client = new Google_Client();

session_start();

$client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$client->setClientSecret('xxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$client->setRedirectUri('http://www.my-website-name.com/drive_test');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    } else
        $client->setAccessToken($_SESSION['access_token']);

    $service = new Google_Service_Drive($client);

    echo "<pre>";
    $all_files = "";
    $all_files = retrieveAllFiles($service);
    print_r($all_files);
    die;

} else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit();
}


/**
 * Retrieve a list of File resources.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @return Array List of Google_Service_Drive_DriveFile resources.
 */
function retrieveAllFiles($service) {
  $result = array();
  $pageToken = NULL;

  do {
    try {
      $parameters = array();
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
  } while ($pageToken);
  return $result;
}

&GT;

我得到这样的输出。

Array
(
)

请帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:1)

哦,是的,最后我找到了问题并修复了它。这是一个许可问题。我只是替换这段代码

$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

使用新代码

$client->setScopes(array('https://www.googleapis.com/auth/drive'));

和everythig