PHP Google Drive API安装和文件上传

时间:2012-10-21 22:46:12

标签: php api codeigniter google-drive-api

大家好,我正在尝试上传G drive API文件。

无法找出它返回错误的原因:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gdrive{

function initialize(){
$credentials = $this->GetOAuth2Credentials($_GET['code']);
$_SESSION['credentials'] = $credentials;

}
/**
 * Exchange an authorization code for OAuth 2.0 credentials.
 *
 * @param String $authorizationCode Authorization code to exchange for an
 *     access token and refresh token.  The refresh token is only returned by
 *     Google on the very first exchange- when a user explicitly approves
 *     the authorization request.
 * @return OauthCredentials OAuth 2.0 credentials object
 */
function GetOAuth2Credentials($authorizationCode) {
  $client = new apiClient();
  $client->setClientId(Config::5112+++++.apps.****5971157@developer.gserviceaccount.com);
  $client->setRedirectUri(Config::site_url());

  /**
   * Ordinarily we wouldn't set the $_GET variable.  However, the API library's
   * authenticate() function looks for authorization code in the query string,
   * so we want to make sure it is set to the correct value passed into the
   * function arguments.
   */
  $_GET['code'] = $authorizationCode;

  $jsonCredentials = json_decode($client->authenticate());

  $oauthCredentials = new OauthCredentials(
      $jsonCredentials->access_token,
      isset($jsonCredentials->refresh_token)?($jsonCredentials->refresh_token):null,
      $jsonCredentials->created,
      $jsonCredentials->expires_in,
      Config::CLIENT_ID,
      Config::CLIENT_SECRET
  );

  return $oauthCredentials;
}
function SaveNewFile($inputFile) {
  try {
    $mimeType = 'text/plain';
    $file = new Google_DriveFile();
    $file->setTitle($inputFile->title);
    $file->setDescription($inputFile->description);
    $file->setMimeType($mimeType);
    // Set the parent folder.
    if ($inputFile->parentId != null) {
      $parentsCollectionData = new DriveFileParentsCollection();
      $parentsCollectionData->setId($inputFile->parentId);
      $file->setParentsCollection(array($parentsCollectionData));
    }
    $createdFile = $this->service->files->insert($file, array(
        'data' => $inputFile->content,
        'mimeType' => $mimeType,
    ));
    return $createdFile;
  } catch (apiServiceException $e) {
    /*
     * Log error and re-throw
     */
    error_log('Error saving new file to Drive: ' . $e->getMessage(), 0);
    throw $e;
  }
}


}

当我调用initialize()方法时,它返回错误:

Message: Undefined index: code

Fatal error: Class 'apiClient' not found

应该是什么?我在我的代码中做得对吗?我需要更多代码才能使其有效吗?我在google api console上创建了web应用程序项目。

我需要包含google php sdk吗?谷歌文档中没有提到谷歌驱动器api:/

2 个答案:

答案 0 :(得分:0)

您可能正在使用旧版本的PHP客户端库。确保您拥有最新的来源,并按照Google云端硬盘SDK快速入门页面中的说明了解如何编写完整的PHP应用以将文件上传到云端硬盘:

https://developers.google.com/drive/quickstart

答案 1 :(得分:0)

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();

请使用那些需要的文件和Google_Client()。