Google Drive access for Service account(服务帐户的 Google 云端硬盘访问权限)
问题描述
我正在尝试从将在 cron 作业中运行的 php 脚本访问我的 Google Drive 文件,但我看不到我的文件.我正在使用在 Google Developpers 控制台中设置的服务帐户,它不需要与用户(我)进行授权交互.这是我的代码:
I am trying to access my Google Drive files from a php script that will be run in a cron job, but I can't see my files. I am using a Service account set up in Google Developpers console, which do not need authorisation interaction with the user (me). This is my code:
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
$client_id = '260186860844-kg9uiqe7pjms3s54gabqfnph0iamjdjn.apps.googleusercontent.com';
$service_account_name = '260186860844-kg9uiqe7pjms3s54gabqfnph0iamjdjn@developer.gserviceaccount.com';
$key_file_location = 'privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Whatever");
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Drive($client);
$parameters = array(
// 'maxResults' => 10,
);
$dr_results = $service->files->listFiles($parameters);
foreach ($dr_results as $item) {
echo $item->title, "<br />
";
}
似乎我已正确连接(获得令牌,没有提示错误),但我只列出了一个文件,该文件不属于我的云端硬盘,名为如何开始使用云端硬盘"!
It seems I am correctly connected (got token, no errors prompted), but I only get one single file listed, which is not part of my Drive, and which is named "How to get started with Drive"!
我正在使用最新的 Google APIs 客户端库 for PHP ,php 5.4
I am using latest Google APIs Client Library for PHP , with php 5.4
推荐答案
Google 将为服务帐户创建自己的 google drive.
Google will create its own google drive for service account.
您需要做的是通过控制台的服务帐户电子邮件共享您的文件夹.
授予它可以编辑"的权限.
Give it permission "can edit".
这篇关于服务帐户的 Google 云端硬盘访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:服务帐户的 Google 云端硬盘访问权限
基础教程推荐
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
