2016-07-08 79 views
-1

我想將我的谷歌驅動器顯示到網站中。這意味着如果有人使用我的網站可以看到谷歌驅動器文件和文件夾沒有登錄。這裏是我試圖實現的一些代碼。Google Drive Api與php的集成

include_once 'google-api-php-client/src/Google/autoload.php'; 
    $scopes = array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.appdata', 
    'https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.metadata','https://www.googleapis.com/auth/drive.metadata.readonly','https://www.googleapis.com/auth/drive.photos.readonly','https://www.googleapis.com/auth/drive.readonly'); 

    /** 
    * Create AssertionCredentails object for use with Google_Client 
    */ 
    $creds = new Google_Auth_AssertionCredentials(
     $serviceAccountName, 
     $scopes, 
     file_get_contents($keyFile) 
    ); 

    $creds->sub = $delegatedAdmin; 

    /** 
    * Create Google_Client for making API calls with 
    */ 
    $client = new Google_Client(); 
    $client->setApplicationName($appName); 
    $client->setClientId($clientId); 
    $client->setAssertionCredentials($creds); 

    /** 
    * Get an instance of the Directory object for making Directory API related calls 
    */ 
    $service = new Google_Service_Drive($client); 

    $optParams = array(
     'pageSize' => 10, 
     'fields' => "nextPageToken, files(id, name)" 
    ); 
    $results = $service->files->listFiles($optParams); 
    print_r($results); exit; 
    /** 

任何人都可以告訴我如何實現這一點。我收到此錯誤

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "unauthorized_client", "error_description" : "Unauthorized client or scope in request." } 

回答

0

您需要添加

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

$client->setAssertionCredentials($creds); 
+0

致命錯誤:未捕獲的異常 'Google_Auth_Exception' 有消息 '錯誤刷新的OAuth2令牌,消息:' {「error」:「unauthorized_client」,「error_description」:「請求中未經授權的客戶端或範圍。」 }'' –

+0

仍然得到相同的錯誤 –

+0

嘗試刪除'$ creds-> sub = $ delegatedAdmin;' –