0
我知道,有数百个职位涉及这一话题的变化,我已经花了几个小时,通过他们读,但我不能得到这个我的谷歌云端硬盘帐户工作。上传文件使用PHP
我想从文件通过谷歌驱动器SDK(我使用V2.2.0)使用PHP我的服务器上传到我的谷歌云端硬盘帐户。
该脚本执行得很好,它似乎在某处上传,甚至返回一个文件ID(始终是同一个),但它没有显示在我的Google云端硬盘中,并且在尝试查看文件时收到以下消息:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "File not found: <FILE_ID>.",
"locationType": "parameter",
"location": "fileId"
}
],
"code": 404,
"message": "File not found: <FILE_ID>."
}
}
我已经设置了服务帐户并下载了JSON凭证。我的目标是创建一个上传存储在我的服务器上的文件的功能,所以我不需要oAuth同意屏幕。
这里的测试代码至今:
<?php
include_once __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=../../../keys/MyProject-xxxxxxxxxxxx.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$client->setApplicationName("MyApplication");
$client->setScopes(['https://www.googleapis.com/auth/drive.file']);
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents('cat.jpg');
$service = new Google_Service_Drive($client);
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'));
echo("<a href='https://www.googleapis.com/drive/v3/files/".$createdFile->id."?key=<MY_API_KEY>&alt=media' target='_blank'>Download</a>");