繁体   English   中英

带有服务帐户的Google Drive API无法正常工作

[英]Google drive API with service account is not working

我正在尝试使用我的服务帐户来实现google drive api。

警告:file_get_contents()[function.file-get-contents]:在第18行的/ home / logic5nv / public_html / projects / google_drive /gservice.php中文件名不能为空致命错误:带有消息的未捕获异常'Google_AuthException'

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL =
'842613324525-6veubhkhrurplpshiuagmdvk1rtm1q1g@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = '8447ce76bbb4956f9cbecd6779371ad3ab8f8e7e-
privatekey.p12';
function buildService() {
    $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
    $auth = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_EMAIL,
    array(DRIVE_SCOPE),$key);
    $client = new Google_Client();
    $client->setUseObjects(true);
    $client->setAssertionCredentials($auth);
    return new Google_DriveService($client);
}
$service = buildService();
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
    'data' => $data,
    'mimeType' => 'text/plain',
));
print_r($createdFile);

尝试:

function buildService() {

global  $SERVICE_ACCOUNT_PKCS12_FILE_PATH,$SERVICE_ACCOUNT_EMAIL,$DRIVE_SCOPE ;

$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
SERVICE_ACCOUNT_EMAIL,
array(DRIVE_SCOPE),$key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
return new Google_DriveService($client);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM