2012-11-14 58 views
2

我一直在試圖實現一個程序,上傳用戶的網站備份到谷歌驅動器。所有的人都對我的域帳戶,所以我通過授權其他的域WDE代表團我的應用程序的步驟就如下所述:https://developers.google.com/drive/delegation谷歌驅動器API域權限授權 - PHP實例化驅動器服務對象錯誤

不幸的是他們的示例代碼實例化一個驅動器服務對象未能在許多層面上。它是:

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

有沒有人對如何解決任何建議或意見這個?如果谷歌這些問題,谷歌只是鏈接到自己的文件,這是我看到前面,沒有在所有的工作頁面後給頁面。

基本上我希望看​​到一個如何使用已被授予域訪問權限來實例化驅動器服務對象的「服務帳戶」的示例。

+0

我也得到了問題。我認爲他們定義了三個變量並使用三個常量! –

+0

是啊,我指出這一點在我的問題。我昨晚做了一些搜索,發現setPrn方法在整個庫中都沒有。我會測試你的答案,並讓你知道它是否工作:)。感謝您及時的回覆。 – pathfinder

回答

4

似乎有一些錯別字文檔中(如果我們寫的文檔,它應該被稱爲錯誤:))。

<?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 protected]'); 


$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. 他們定義了三個varivbales但使用三三個constants-刪除了contsnts和使用的變量來代替。

  2. 沒有方法Google_AssertionCredentials::setPrn()。物業prn的知名度是公開的。所以你可以將它設置爲$auth->prn = $userEmail;

+0

謝謝habeeb,這個作品很棒! – pathfinder

+1

謝謝!我將更改Drive文檔以使用$ auth-> prn而不是setPrn! – Nivco

+0

感謝這個,真的幫了出來。 – dev