【问题标题】:Google Drive API Domain Wide Delegation of Authority - PHP instantiate a drive service object errorsGoogle Drive API 域范围授权 - PHP 实例化驱动器服务对象错误
【发布时间】:2012-11-14 23:39:37
【问题描述】:

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

不幸的是,他们用于实例化驱动服务对象的示例代码在许多层面上都失败了。这里是:

<?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);
}

?>

第一个明显的错误是他们设置了变量,但函数使用了常量。所以我硬编码了常量(KEY_FILE、SERVICE_ACCOUNT_EMAIL 等)应该有的内容,只是为了看看它是否有效,然后我收到以下错误:

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

是否有人对如何解决此问题有任何建议或 cmet?如果你用谷歌搜索这些问题,谷歌只会提供一页又一页的链接到他们自己的文档,正如我在上面显示的那样,它根本不起作用。

基本上,我希望看到一个示例,说明如何使用已被授予域范围访问权限的“服务帐户”来实例化驱动服务对象。

【问题讨论】:

  • 我也遇到了这个问题。我认为他们定义了三个变量并使用了三个常量!
  • 是的,我在我的问题中指出了这一点。而且我昨晚做了一些搜索,发现整个库中都不存在 setPrn 方法。我会测试你的答案,让你知道它是否有效:)。感谢您的快速回复。

标签: php google-drive-api


【解决方案1】:

文档中好像有一些错别字(如果我们写了doc,应该叫bug :))。

<?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. 他们定义了三个变量,但使用了三个三个常量 - 删除了常量并改用变量。

  2. 没有方法Google_AssertionCredentials::setPrn()。属性prn 的可见性是公开的。所以可以设置为$auth-&gt;prn = $userEmail;

【讨论】:

  • 谢谢!我将更改 Drive 文档以使用 $auth->prn 而不是 setPrn !
  • 谢谢你,真的帮了大忙。
猜你喜欢
  • 1970-01-01
  • 2019-12-21
  • 2020-04-02
  • 1970-01-01
  • 2014-09-26
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多