use*_*961 2 php google-api google-drive-api google-oauth
我想写一个服务器php脚本,它将访问我的谷歌驱动器并在那里复制一个文件.服务器必须保存我的谷歌驱动器的凭据,而不是要求授权.我看到的所有示例都描述了各种用户可以在其驱动器上执行操作的Web应用程序.例如,https://developers.google.com/drive/v3/web/quickstart/php 如何在我的服务器上保存所有需要的凭据.
经过长时间的研究和阅读谷歌文档和示例,我发现了一种适合我的方式.
最后这是工作代码片段:
<?php
require_once '/path/to/google-api-php-client/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$client->setSubject('email_of_account@you_want_to_work.for');
$service = new Google_Service_Drive($client);
//Create a new folder
$fileMetadata = new Google_Service_Drive_DriveFile(
array('name' => 'Invoices',
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));
echo $file->id;
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7408 次 |
| 最近记录: |