在通过Google Drive API上传的谷歌驱动器中找不到我的文件
问题描述:
我正在将文件上传到我的谷歌驱动器帐户。这是代码:在通过Google Drive API上传的谷歌驱动器中找不到我的文件
putenv('GOOGLE_APPLICATION_CREDENTIALS=xxxx.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Drive::DRIVE);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
$tempfile = "birds-image-11.jpg";
$file = new Google_Service_Drive_DriveFile(array(
'name' => 'tc-test.jpg',
));
$service = new Google_Service_Drive($client);
$result = $service->files->create($file, array(
'data' => file_get_contents($tempfile),
'mimeType' => 'application/octet-stream',
'uploadType' => 'media',
'fields' => 'id, name,kind'
));
printf("File ID: %s\n", $result->id);echo '<br>';
printf("File Name: %s\n", $result->name);echo '<br>';
printf("File Kind: %s\n", $result->kind); echo '<br>';
我可以看到id,名称和返回的种类。那就是说文件已经上传了吗?但是,当我转到我的谷歌驱动器,我看不到这些文件。
如果我列出来自api $service->files->listFiles();
的文件,我可以看到上传的文件对象返回。
为什么我看不到它在我的驱动器(https://drive.google.com/drive/my-drive)和我应该在哪里查找文件?
顺便说一句,我使用的服务帐户的API。
答
您还需要设置文件的父文件夹,该文件夹可以是用户的“我的驱动器”或任何其他文件夹。
Specify folder name while upload file via Google Drive API
https://developers.google.com/drive/v3/reference/files/update
'运行putenv( 'GOOGLE_APPLICATION_CREDENTIALS =' xxxx.json');'
@MagnusEriksson,已修复。 –