无法获取Google Drive';的文件或其元数据


Unable to get Google Drive's file or its metadata on my Webstore App

我希望用户使用我的Chrome网店应用程序打开一个文件,然后我将处理我获得的文件和元数据。我一直在关注谷歌的官方文档,但我无法实现它。

我从Github的Google api php客户端添加了库,这是我一直在尝试的代码之一:

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('xxxx.apps.googleusercontent.com');
$client->setClientSecret('xxxxx');
$client->setRedirectUri('https://my_site.com/path/driveapp.php');
$client->setScopes(array(
  'https://www.googleapis.com/auth/drive',
  'email',
  'profile'));
$client->setUseObjects(true);
// if there is an existing session, set the access token
if ($user = get_user()) {
  $client->setAccessToken($user->tokens);
}
// initialize the drive service with the client.
$service = new Google_DriveService($client);
/**
 * Gets the metadata and contents for the given file_id.
 */
$app->get('/svc', function() use ($app, $client, $service) {
  checkUserAuthentication($app);
  checkRequiredQueryParams($app, array('file_id'));
  $fileId = $app->request()->get('file_id');
  try {
    // Retrieve metadata for the file specified by $fileId.
    $file = $service->files->get($fileId);
    // Get the contents of the file.
    $request = new Google_HttpRequest($file->downloadUrl);
    $response = $client->getIo()->authenticatedRequest($request);
    $file->content = $response->getResponseBody();
    renderJson($app, $file);
  } catch (Exception $ex) {
    renderEx($app, $ex);
  }
});

我一直在遵循我应该遵循的所有步骤;去控制台,配置了Drive SDK,启用了Drive API,创建了客户端ID,关心我的Chrome web-app的json中的参数。但在使用文档中提到的方法后,我仍然会不断出现语法错误。

据我所知,文档并没有更新,因此库文件的路径存在问题。

PS:我已经检查了Quickstart、Authorize Requests和examples的代码,但没有任何帮助。

使用最新的PHP库驱动api很棘手。网络上也缺乏文档。这个代码对我有效,它应该会帮助你:

<?php
session_start();
require_once 'google-api-php-client/src/Google/autoload.php';

$client_id = '';
$client_secret = '';
$redirect_uri = '';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
function printFile($service, $fileId) {
  try {
    $file = $service->files->get($fileId);
    print "Title: " . $file->getTitle();
    print "Description: " . $file->getDescription();
    print "MIME type: " . $file->getMimeType();
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['upload_token']);
}
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['upload_token'] = $client->getAccessToken();
}
if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
  $client->setAccessToken($_SESSION['upload_token']);
  if ($client->isAccessTokenExpired()) {
    unset($_SESSION['upload_token']);
  }
} else {
  $authUrl = $client->createAuthUrl();
}
if ($client->getAccessToken()) {
  // This is uploading a file directly, with no metadata associated.
  if (isset($_GET['state'])) {
    $a = urldecode(urldecode($_GET['state']));
    $state = json_decode(stripslashes($a));
    $_SESSION['mode'] = $state->action;
    if (isset($state->ids)){
      $_SESSION['fileIds'] = $state->ids;
    } else {
      $_SESSION['fileIds'] = array();
    }
    if (isset($state->parentId)) {
      $_SESSION['parentId'] = $state->parentId;
    } else {
      $_SESSION['parentId'] = null;
    }        
    $fileId = $_SESSION['fileIds'];
    printFile($service, $fileId[0]);
}

 }
?>