2017-05-12 88 views
0

我在嘗試構建一個CMS,它使用Google Drive帳戶作爲存儲並顯示其內容,文件/文件夾,用戶應能夠通過我的管理文件Angular FrontEnd。我對google-api很陌生,認爲我試了一下,但我被卡住了。PHP google-api-php-client獲取用戶文件結構

我正在使用此庫在本地xampp中使用PHP來工作。

Git Google PHP API

我試圖讓在用戶文件以.php測試文件這樣的迴應:

/************************************************* 
* Ensure you've downloaded your oauth credentials 
************************************************/ 
if (!$oauth_credentials = getOAuthCredentialsFile()) 
{ 
    echo missingOAuth2CredentialsWarning(); 
    return; 
} 

/************************************************ 
* The redirect URI is to the current page, e.g: 
* http://localhost:8080/simple-file-upload.php 
************************************************/ 
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 

$client = new Google_Client(); 
$client->setAuthConfig($oauth_credentials); 
$client->setRedirectUri($redirect_uri); 
$client->addScope(Google_Service_Drive::DRIVE); 
$service = new Google_Service_Drive($client); 


$pageToken = null; 
do { 
    $response = $service->files->listFiles(); 

    foreach ($response->files as $file) { 
     printf("Found file: %s (%s)\n", $file->name, $file->id); 
    } 
} while ($pageToken != null); 

但是,這將引發致命錯誤:未捕獲Google_Service_Exception:{ 「錯誤」:{ 「錯誤」:[{「domain」:「usageLimits」,「reason」:「dailyLimitExceededUnreg」,「message」:「每日限制未經驗證的使用超過。繼續使用需要註冊。」,

我得到git倉庫工作。但我沒有找到一個有用的例子來查詢用戶文件結構。

+0

我以前有這個錯誤,我當我發送的請求未通過身份驗證時發生。 – Morfinismo

+0

該庫中有多個身份驗證(OAuth,服務帳戶),此類請求需要什麼,或者它甚至很重要?當我嘗試上傳示例時,系統會要求我登錄併成功將文件上傳到驅動器。如果我將身份驗證部分複製到此查詢文件,它不起作用。 – AHahn

回答

1

我找到了我在我的測試應用程序是丟失: 設置訪問令牌這樣的 - >

if (isset($_GET['code'])) 
{ 
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); 
    $client->setAccessToken($token); //really needed? we also set it when the session is not empty 

    // store in the session also 
    $_SESSION['upload_token'] = $token; 

    // redirect back to the example 
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
} 

// set the access token as part of the client 
if (!empty($_SESSION['upload_token'])) 
{ 
    $client->setAccessToken($_SESSION['upload_token']); 
    if ($client->isAccessTokenExpired()) 
    { 
     unset($_SESSION['upload_token']); 
    } 
} 
else 
{ 
    $authUrl = $client->createAuthUrl(); 
} 

創建一個服務,如客戶端 - >

$service = new Google_Service_Drive($client); 

最後,重要部分:只有訪問它,如果我們登錄

/************************************************ 
* If we're signed in then lets try to do something 
************************************************/ 
if ($client->getAccessToken()) 
{ 
    $response = $service->files->listFiles(); 

    foreach ($response->files as $file) 
    { 
     echo "<div>" . $file->name . " - " . $file->id . "</div><br>"; 
    } 
}