このトピックのバリエーションを網羅した数百の記事がありますが、過去数時間を読んだことがありますが、作業。PHPを使用してGoogleドライブアカウントにファイルをアップロードする
Googleドライブ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>");