簡體   English   中英

如何使用 php Google Drive API 在 Google Drive 上上傳文件

[英]How to upload a file on google drive with php Google Drive API

我想用 php API 在我的 Google Drive 中添加一個文件。 我在谷歌控制台和谷歌管理員中正確設置了服務帳戶權限。 我的代碼如下:

$client = new Google_Client();
$client->setAuthConfig('path.json');
$client->addScope('https://www.googleapis.com/auth/drive');

$service = new Google_Service_Drive($client);

//Insert a file
$fileMetadata = new Google_Service_Drive_DriveFile(array('name' => 'test.txt'));
$content = file_get_contents('test.txt');
$file = $service->files->create($fileMetadata, array(    'data' => $content, 'mimeType' => 'text/plain', 'uploadType' => 'multipart', 'fields' => 'id'));
print($file->id);

它返回 id: 1otqEi5qaJ8SDjcPMIFOWtMQVPXZKlaSb 但是我的驅動器上不存在該文檔...

感謝幫助...

服務帳戶不是您。 服務帳戶有自己的 google drive 帳戶。 該文件正在上傳到其 google drive 帳戶。

如果您希望它上傳到您的 google drive 帳戶,請使用服務帳戶 email 地址添加與它共享您的 google drive 帳戶中的目錄。 然后獲取該文件夾的文件 ID/驅動器 ID,並在您上傳文件時將其設置為文件元數據中的父級,它將上傳到您的驅動器帳戶而不是服務帳戶。

請記住讓服務帳戶在上傳該文件后授予您對該文件的權限,該文件將歸服務帳戶所有。

暫無
暫無

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

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