简体   繁体   English

PHP Google Drive API安装和文件上传

[英]PHP Google Drive API installation and file upload

Hi guys i'm trying uploading file trought G drive API. 嗨,大家好,我正在尝试上传文件格式的G驱动器API。

Can't find out why it returns error: 无法找到为什么返回错误:

<?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;
  }
}


}

when i invoke the initialize() method it returns error: 当我调用initialize()方法时,它返回错误:

Message: Undefined index: code

Fatal error: Class 'apiClient' not found

what should be? 应该是什么? i'm doing right in my code ? 我的代码正确吗? does i need more code to make it works? 我需要更多代码才能使其正常工作吗? i created web application project on google api console. 我在Google api控制台上创建了Web应用程序项目。

need i to include google php sdk? 我需要包括Google PHP SDK吗? in the google docs it is not mentioned for google drive api :/ 在google docs中未提及google drive api:/

You are probably using an older version of the PHP client library. 您可能正在使用旧版本的PHP客户端库。 Make sure you have the latest source and follow the instructions in the Google Drive SDK quickstart page to learn how to write a complete PHP app to upload a file to Drive: 确保您拥有最新资源,并按照Google Drive SDK快速入门页中的说明进行操作,以了解如何编写完整的PHP应用程序以将文件上传到Drive:

https://developers.google.com/drive/quickstart 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();

please use those require files and Google_Client(). 请使用这些必填文件和Google_Client()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM