1

大家好,我正在尝试通过 G 驱动器 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 控制台上创建了 Web 应用程序项目。

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

4

2 回答 2

0

您可能正在使用旧版本的 PHP 客户端库。确保您拥有最新的源代码并按照 Google Drive SDK 快速入门页面中的说明学习如何编写完整的 PHP 应用程序以将文件上传到 Drive:

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

于 2012-10-22T00:48:24.420 回答
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()。

于 2013-02-28T22:43:04.300 回答