【发布时间】:2019-08-08 21:56:24
【问题描述】:
我一直在尝试通过 service-account 使用 php 将文件上传到我的 Google Drive 帐户。我猜文件已成功上传,因为我可以打印文件 ID、文件创建时间和所有其他信息。
这是我的问题:当我登录我的 google drive 帐户时,我看不到任何上传的文件。文件隐藏在哪里或者我该怎么办?
代码如下:
require __DIR__ . '/vendor/autoload.php'; //api
$serviceAccount = "xxx@xxxxxx.iam.gserviceaccount.com";
$key_file = "mykey-goes-here.p12";
$auth = new Google_Auth_AssertionCredentials(
$serviceAccount,
array('https://www.googleapis.com/auth/drive.file'),
file_get_contents($key_file)
);
$client = new Google_Client();
$client->setAssertionCredentials( $auth );
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle("my file name");
$file->setDescription('testing desc');
$file->setMimeType('text/plain');
// Provide file name here and path in below
$result = $service->files->insert($file, array(
'data' => file_get_contents("test.txt"),
//'mimeType' => 'text/plain',
'mimeType' => 'application/octet-stream',
'uploadType' => 'media',
//'uploadType' => 'multipart'
));
// Uncomment the following line to print the File ID
printf("File ID: %s\n", $result->id);
echo "<br><br>";
echo '<pre>'; print_r($result); echo'</pre>';
【问题讨论】:
-
如果手动上传文件,可以用PHP代码访问吗?
标签: php google-drive-api