簡體   English   中英

PHP Google Drive API安裝和文件上傳

[英]PHP Google Drive API installation and file upload

嗨,大家好,我正在嘗試上傳文件格式的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應用程序項目。

我需要包括Google PHP SDK嗎? 在google docs中未提及google drive api:/

您可能正在使用舊版本的PHP客戶端庫。 確保您擁有最新資源,並按照Google Drive SDK快速入門頁中的說明進行操作,以了解如何編寫完整的PHP應用程序以將文件上傳到Drive:

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

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()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM