簡體   English   中英

如何使用PHP使用Google Drive API正確定義Google_Service_Drive變量來創建文件夾?

[英]How can I define a Google_Service_Drive variable correctly with Google Drive API using PHP to create a folder?

我是新手,因此希望您能對我有所幫助,我正在檢查Google Drive API文檔,因為我想使用PHP在Google Drive中創建一個文件夾,我使用的是代碼示例,但我注意到我必須首先定義此“ $ driveService”變量。

我必須說我之前已經完成了前面的步驟(創建項目,通過composer安裝,生成我的client_secret.json文件等)。

因此,我嘗試使用編寫以下代碼的示例:

<?php
 require_once __DIR__.'/vendor/autoload.php';

 $client = new Google_Client();
 $client->setAuthConfigFile('64*****-client_secret.json');
 $client->setRedirectUri('https://www.mywebsite.com/drive/oauth2callback.php');
 $client->addScope(Google_Service_Drive::DRIVE);

 $driveService = new Google_Service_Drive($client);
 $fileMetadata = new Google_Service_Drive_DriveFile(array(
   'name' => 'TEST',
   'mimeType' => 'application/vnd.google-apps.folder'));

 $file = $driveService->files->create($fileMetadata, array('fields' => 'id'));

 printf("Folder ID: %s\n", $file->id);

?>

但是,當我執行該頁面時,它在瀏覽器中什么也沒顯示,而是在終端中顯示:

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with 
 message '{
  "error": {
  "errors": [
  {
   "domain": "global",
   "reason": "required",
   "message": "Login Required",
   "locationType": "header",
   "location": "Authorization"
  }
 ],
 "code": 401,
 "message": "Login Required"
 }
}
' in /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php:118
Stack trace: #0 /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array) #3 /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run() in /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php on line 118

我還添加了以下幾行:

 $client = new Google_Client();
 $client->setAuthConfigFile('64*****-client_secret.json');
 $client->setRedirectUri('https://www.mywebsite.com/drive/oauth2callback.php');
 $client->setAuthConfig('credentials.json');
 $client->setAccessType('offline');
 $client->addScope(Google_Service_Drive::DRIVE);

但是仍然一樣,我無法創建文件夾並獲取文件夾ID。 我該如何解決?

謝謝您的回答。

您需要先獲取訪問令牌; 用戶需要通過用戶同意屏幕授予您對其驅動器文件的訪問權限。 喲可以試試看;

 $client = new Google_Client();
 $client->setAuthConfigFile('64*****-client_secret.json');
 $client->setRedirectUri('https://www.mywebsite.com/drive/oauth2callback.php');
 $client->addScope(Google_Service_Drive::DRIVE);

         /** checks if the access token has previously been obtained and sets it **/
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']){
    $client->setAccessToken($_SESSION['access_token']);
     //what ever code you want to run
      else{
     //redirects the user to authenticate
  $redirect_uri = 'https://www.mywebsite.com/drive/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }

    }

$ client-> setAuthConfig('credentials.json'); 您添加的是重復項,沒有必要。 同樣要記住在這樣的代碼開頭調用會話開始:session_start();

有關詳細信息,請訪問: https : //developers.google.com/identity/protocols/OAuth2WebServer

暫無
暫無

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

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