Google Drive API Domain Wide Authority - PHP实例化驱动器服务对象错误 [英] Google Drive API Domain Wide Delegation of Authority - PHP instantiate a drive service object errors

查看:292
本文介绍了Google Drive API Domain Wide Authority - PHP实例化驱动器服务对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试实施一个程序,将用户网站的备份上传到谷歌驱动器。他们都在我的域名上有一个帐户,所以我按照下面的描述完成了为我的应用授予域名权限授权的步骤: https://developers.google.com/drive/delegation



不幸的是,它们用于实例化驱动器服务对象的示例代码在许多水平。这里是:

 <?php 

require_oncegoogle-api-php-client / SRC / Google_Client.php;
require_oncegoogle-api-php-client / src / contrib / Google_DriveService.php;
require_oncegoogle-api-php-client / src / contrib / Google_Oauth2Service.php;
session_start();

$ DRIVE_SCOPE ='https://www.googleapis.com/auth/drive';
$ SERVICE_ACCOUNT_EMAIL ='< some-id> @ developer.gserviceaccount.com';
$ SERVICE_ACCOUNT_PKCS12_FILE_PATH ='privatekey.p12';

/ **
*建立并返回一个驱动服务对象
*,授权服务帐户
*代表给定用户。
*
* @param userEmail用户的电子邮件。
* @返回Google_DriveService服务对象。
* /
函数buildService($ userEmail){
$ key = file_get_contents(KEY_FILE);
$ auth = new Google_AssertionCredentials(
SERVICE_ACCOUNT_EMAIL,
array(DRIVE_SCOPE),
$ key);
$ auth-> setPrn($ userEmail);
$ client = new Google_Client();
$ client-> setUseObjects(true);
$ client-> setAssertionCredentials($ auth);
返回新的Google_DriveService($ client);
}

?>

第一个明显的错误是他们设置了变量,但是函数使用了常量。所以我硬编码的常量(KEY_FILE,SERVICE_ACCOUNT_EMAIL等)应该在那里,看看它是否工作,然后我得到以下错误:

 致命错误:调用未定义的方法Google_AssertionCredentials :: setPrn()

有没有人对如何解决这个问题有任何建议或意见?如果你谷歌这些问题,谷歌只是给他们自己的文件,这正如我上面显示的链接页面,并没有工作。



基本上我希望以查看如何使用已授予域范围访问权限的服务帐户来实例化驱动器服务对象的示例。 解决方案

p>似乎在文档中有一些拼写错误(如果我们写了文档,它应该称为错误:))。

 <?php 

require_oncegoogle-api-php-client / src / Google_Client.php;
require_oncegoogle-api-php-client / src / contrib / Google_DriveService.php;
require_oncegoogle-api-php-client / src / contrib / Google_Oauth2Service.php;
session_start();

函数buildService($ userEmail){

$ DRIVE_SCOPE ='https://www.googleapis.com/auth/drive';
$ SERVICE_ACCOUNT_EMAIL ='< some-id> @ developer.gserviceaccount.com';
$ SERVICE_ACCOUNT_PKCS12_FILE_PATH ='privatekey.p12';

$ key = file_get_contents($ SERVICE_ACCOUNT_PKCS12_FILE_PATH);

$ auth = new Google_AssertionCredentials($ SERVICE_ACCOUNT_EMAIL,array($ DRIVE_SCOPE),$ key); //改变了!

$ auth-> prn = $ userEmail; //改变了!

$ client = new Google_Client();
$ client-> setUseObjects(true);
$ client-> setAssertionCredentials($ auth);
返回新的Google_DriveService($ client);
}

$ service = buildService('email@yourdomain.com');


$ file = new Google_DriveFile();
$ file-> setTitle('我的文档');
$ file-> setDescription('测试文档');
$ file-> setMimeType('text / plain');

$ data =contents; ($ data,'mimeType'=>'text / plain',)) ;

print_r($ createdFile);




  1. 他们定义了三个varivbales,但使用了三个三个常量 - Removed并且使用了变量。

  2. 没有方法 Google_AssertionCredentials :: setPrn()。属性 prn 的可见性是公开的。因此,您可以将它设置为 $ auth-> prn = $ userEmail;



I have been trying to implement a program that uploads backups of my user's websites to google drive. All of them have an account on my domain, so I went through the steps of granting domain wde delegation of authority for my app as described here: https://developers.google.com/drive/delegation

Unfortunately their sample code to instantiate a drive service object fails on many levels. Here it is:

<?php

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 = '<some-id>@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'privatekey.p12';

/**
 * Build and returns a Drive service object 
 * authorized with the service accounts
 * that acts on behalf of the given user.
 *
 * @param userEmail The email of the user.
 * @return Google_DriveService service object.
 */
function buildService($userEmail) {
  $key = file_get_contents(KEY_FILE);
  $auth = new Google_AssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      array(DRIVE_SCOPE),
      $key);
  $auth->setPrn($userEmail);
  $client = new Google_Client();
  $client->setUseObjects(true);
  $client->setAssertionCredentials($auth);
  return new Google_DriveService($client);
}

?>

The first obvious error is they have you set up variables but then the function uses constants. So I hardcoded in what should be there for the constants (KEY_FILE, SERVICE_ACCOUNT_EMAIL, etc) just to see if it worked and then I get the following error:

Fatal error: Call to undefined method Google_AssertionCredentials::setPrn()

Does anyone have any suggestions or comments on how to fix this? If you google these issues, google just gives page after page of links to their own documentation, which as I show above, does not work at all.

Basically I was hoping to see an example of how to use a "service account" which has been granted domain wide access to instantiate a drive service object.

解决方案

It seems that there are some typos (If we wrote the doc, it should be called bug :) ) in the documentation.

<?php

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();

function buildService($userEmail) {

  $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
  $SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com';
  $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'privatekey.p12';

  $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);

  $auth = new Google_AssertionCredentials($SERVICE_ACCOUNT_EMAIL, array($DRIVE_SCOPE), $key); // Changed!

  $auth->prn = $userEmail; // Changed!

  $client = new Google_Client();
  $client->setUseObjects(true);
  $client->setAssertionCredentials($auth);
  return new Google_DriveService($client);
}

$service = buildService('email@yourdomain.com');


$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = "contents";

$createdFile = $service->files->insert($file, array('data' => $data,'mimeType' =>'text/plain',));

print_r($createdFile);

  1. They defined three varivbales but used three three constants- Removed the contsnts and used the variables instead.

  2. There is no method Google_AssertionCredentials::setPrn(). The property prn's visibility is public. So you can set it as $auth->prn = $userEmail;

这篇关于Google Drive API Domain Wide Authority - PHP实例化驱动器服务对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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