2014-07-22 139 views
1

我使用谷歌API驅動器創建網站的頁面裏面做以下的東西: -谷歌驅動SDK /谷歌驅動API ----使用PHP的Web應用程序

提供用戶設置存儲在我的谷歌PDF文件的通過任何方式驅動沒有任何類型的登錄/身份驗證,文件是公開的。 誰訪問該頁面,如果他/她想要下載該文件/ pdf,然後他/她可以通過點擊它而沒有任何註冊和登錄。

我有簡單的單詞不知道如何開始使用它......是有必要使用OAuth 2 ...

我想使用谷歌驅動器作爲文件託管網站主辦我的文件,並達到用戶通過網站。 請給我你的寶貴的解決方案... 謝謝

+0

你使用什麼編程語言或平臺? – dsum27

回答

0

對於PHP看看here爲一個完整的例子和視頻。您必須下載適用於您正在使用的編程語言的Google Drive API。然後使用該API來幫助您驗證和訪問文件。下面是一個使用PHP

<?php 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; 

$client = new Google_Client(); 
// Get your credentials from the console 
$client->setClientId('YOUR_CLIENT_ID'); 
$client->setClientSecret('YOUR_CLIENT_SECRET'); 
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); 
$client->setScopes(array('https://www.googleapis.com/auth/drive')); 

$service = new Google_DriveService($client); 

$authUrl = $client->createAuthUrl(); 

//Request authorization 
print "Please visit:\n$authUrl\n\n"; 
print "Please enter the auth code:\n"; 
$authCode = trim(fgets(STDIN)); 

// Exchange authorization code for access token 
$accessToken = $client->authenticate($authCode); 
$client->setAccessToken($accessToken); 

//Insert a file 
$file = new Google_DriveFile(); 
$file->setTitle('My document'); 
$file->setDescription('A test document'); 
$file->setMimeType('text/plain'); 

$data = file_get_contents('document.txt'); 

$createdFile = $service->files->insert($file, array(
     'data' => $data, 
     'mimeType' => 'text/plain', 
    )); 

print_r($createdFile); 
?>