繁体   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