谷歌驱动PHP权限

时间:2014-06-02 14:33:42

标签: php google-drive-api google-api-php-client

我想通过谷歌服务帐户帮助分享谷歌硬盘上某些文件夹的权限,但致命错误:未捕获的异常' Google_Service_Exception'有消息'错误调用POST https://www.googleapis.com/drive/v2/files/0ByXNGBI2AHyZX0RZd2pROE5qNFk/permissions :( 500)内部错误' 它的问题仅在使用服务帐户类型身份验证时才会出现

当我粘贴更多范围array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file'),我重新接收404错误文件未找到0ByXNGBI2AHyZX0RZd2pROE5qNFk

 <?php
session_start();
include_once "templates/base.php";

/************************************************
Make an API request authenticated with a service
account.
 ************************************************/
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client_id = '1077677978854-deaidfbbb36erpstbjsvqh6s1iftg36k.apps.googleusercontent.com';
$service_account_name = '1077677978854-deaidfbbb36erpstbjsvqh6s1iftg36k@developer.gserviceaccount.com';
$key_file_location = './8c320ff616a77aec2421642de64449dad6f4a9ca-privatekey.p12';

$client = new Google_Client();

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();
var_dump($client);
$service = new Google_Service_Drive($client);
$newPermission= new Google_Service_Drive_Permission();
$newPermission->setType('user');
$newPermission->setRole('reader');
$newPermission->setValue('rizr19911006@gmail.com'); //thats email to share
$service->permissions->insert('0ByXNGBI2AHyZX0RZd2pROE5qNFk',$newPermission);

1 个答案:

答案 0 :(得分:0)

您应该使用$ service_account_name电子邮件地址手动共享该文件。