douxiajiao8445 2012-11-14 23:39
浏览 47
已采纳

Google Drive API域范围授权 - PHP实例化驱动器服务对象错误

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.

  • 写回答

1条回答 默认 最新

  • dongsong8932 2012-11-15 11:19
    关注

    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;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题