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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记