Google云端硬盘访问服务帐号 [英] Google Drive access for Service account

查看:197
本文介绍了Google云端硬盘访问服务帐号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从php脚本访问我的Google Drive文件,该脚本将在cron作业中运行,但我看不到我的文件。
我使用的是Google开发者控制台中设置的服务帐户,该帐户不需要与用户(我)进行授权交互。
这是我的代码:

  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 /> \\\
;
}

看起来我连接正确(获得令牌,没有错误提示),但我只收到一个列出的单个文件,这不是我的云端硬盘的一部分,名为如何开始使用云端硬盘!

我正在使用最新的用于PHP的Google API客户端库,使用php 5.4


<解决方案

谷歌将打造自己的谷歌驱动器的服务帐户。



您需要做的是使用服务器帐户电子邮件从控制台共享您的文件夹。



给它许可可以编辑。


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 /> \n";
}

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"!

I am using latest Google APIs Client Library for PHP , with php 5.4

解决方案

Google will crate its own google drive for service account.

Whan you need to do is SHARE your folder with service's account email from console.

Give it permission "can edit".

这篇关于Google云端硬盘访问服务帐号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆