我知道有数百个帖子涉及这个主题的变体,我花了最后几个小时阅读它们,但我无法让它工作。
我想通过Google Drive SDK使用PHP从我的服务器上传文件到我的Google云端硬盘帐户(我使用的是v2.2.0)。
脚本执行正常,它似乎在某个地方上传,甚至返回一个文件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>");
答案 0 :(得分:1)
解决!
您需要在Google云端硬盘中创建一个文件夹,然后将服务帐户电子邮件地址添加到可以添加&amp;的人员列表中。编辑。
然后您需要在代码中指定文件夹ID:
$file->setParents(array("0B_xxxxxxxxxxxxxxxxxxxxxxxxx"));