沃梦达 / 编程问答 / php问题 / 正文

服务帐户的 Google 云端硬盘访问权限

Google Drive access for Service account(服务帐户的 Google 云端硬盘访问权限)

本文介绍了服务帐户的 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 云端硬盘访问权限

基础教程推荐