2014-12-13 52 views
1

當我從php運行API時,返回的唯一項目是「如何開始使用Drive」文件,該文件不在文件夾中,但是當我從API運行retrieveListOfAllFiles()網頁我看到驅動器中的所有文件。這是代碼。Google_Service_Drive retrieveListOfAllFiles只返回一個文件

set_include_path(ABSPATH . "path-to/api/google-api-php-client/src/"); 
require_once "Google/Client.php"; 
require_once "Google/Service/Drive.php"; 
require_once "Google/Auth/OAuth2.php"; 
require_once "Google/Auth/AssertionCredentials.php"; 

    define('CLIENT_ID', 'xxxxx'); 
    define('SERVICE_ACCOUNT_NAME', 'xxxxx'); 
    $key = file_get_contents(ABSPATH . "path-to-.p12"); 
    $scopes = array('https://www.googleapis.com/auth/drive.readonly'); 


    $client = new Google_Client(); 
    $client->setApplicationName("xxxx"); 

    if (isset($_SESSION['service_token'])) { 
    $client->setAccessToken($_SESSION['service_token']); 
    } 

    $auth = new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    $scopes, 
    $key 
); 

    $client->setAssertionCredentials($auth); 

    if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($auth); 
    } 

    $_SESSION['service_token'] = $client->getAccessToken(); 

    $service = new Google_Service_Drive($client); 

    $files = retrieveAllFiles($service); 

文件只返回了「如何開始使用驅動器」文件 - 它從這個頁面的API測試:https://developers.google.com/drive/v2/reference/files/list回報所有文件的驅動器。

該ID是正確的,該.p12文件是新的和加載,我不知道什麼是錯的。

我也注意到,如果我的var_dump()的這個

dump($service->files->listFiles(array())); 

輸出我也只能得到單一的「如何開始使用驅動器」文件。因此,即使我沒有收到任何OAuth2錯誤,並且獲得密鑰令牌等,我想列出文件所在的驅動器也未被訪問。

回答

7

問題已解決。雲端硬盤中的文件夾必須與API用戶共享 - 電子郵件以@ developer.gserviceaccount.com結尾 - 我沒有在任何地方看到文檔中的內容......也許我是盲人。但是這些API文檔似乎過時了。

+0

非常感謝...這個答案幫助我... – 2016-07-08 09:47:26