无法在我的网上应用店中获取Google云端硬盘的文件或其元数据

时间:2015-03-27 12:50:07

标签: php google-drive-api google-chrome-app google-api-php-client

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

我添加了Github's Google-api-php-client中的库,这是我一直在尝试的代码之一:

$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网络应用json中的参数。但是在使用文档中提到的方法后,我仍然会遇到语法错误。

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

PS:我已经检查了QuickstartAuthorize Requestsexamples'代码,但没有任何帮助。

1 个答案:

答案 0 :(得分:1)

使用最新的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]);
}



 }

?>