4

我需要幫助做出正確的決定如何使用谷歌API。PHP腳本訪問服務器到服務器,以Google驅動器顯示我自己的文件

我想開發一個腳本來連接服務器到服務器(沒有任何Oauth2連接)到我的谷歌驅動器文件夾,以便在網頁中顯示谷歌驅動器文件和文件夾。如果某些文件已更新,我還會設置一個cron作業來執行操作。

這可能嗎? 在谷歌雲平臺上,我發現了服務器到服務器交互的驅動器api。這是正確的解決方案嗎?

謝謝你!

+1

查找服務帳戶。只要該服務帳戶可以訪問您的雲端硬盤帳戶上的文件(像添加它,就可以與任何其他用戶分享),它將能夠訪問它。然後查看https://developers.google.com/drive/v2/reference/files/watch – DaImTo

+0

它需要oauth或公共文件。這不可能是我的方式。感謝您的幫助! – Luca

+0

服務帳戶是Oauth是的,但它的預授權Oauth。所以你不會得到彈出窗口,要求用戶授予你訪問權限,這就是爲什麼它是理想的服務器應用程序,如cron作業。該文件不必在谷歌驅動器上公開,你只需授予服務帳戶電子郵件地址訪問該文件就像任何其他用戶(這是預授權部分) – DaImTo

回答

6

https://developers.google.com/drive/v3/web/quickstart/php

在這裏,你會發現啓用API的方式。從這一點你可以使用圖書館。

該網站將提供一個樣本:

<?php 
 
require __DIR__ . '/vendor/autoload.php'; 
 

 
define('APPLICATION_NAME', 'Drive API PHP Quickstart'); 
 
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json'); 
 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 
 
// If modifying these scopes, delete your previously saved credentials 
 
// at ~/.credentials/drive-php-quickstart.json 
 
define('SCOPES', implode(' ', array(
 
    Google_Service_Drive::DRIVE_METADATA_READONLY) 
 
)); 
 

 
if (php_sapi_name() != 'cli') { 
 
    throw new Exception('This application must be run on the command line.'); 
 
} 
 

 
/** 
 
* Returns an authorized API client. 
 
* @return Google_Client the authorized client object 
 
*/ 
 
function getClient() { 
 
    $client = new Google_Client(); 
 
    $client->setApplicationName(APPLICATION_NAME); 
 
    $client->setScopes(SCOPES); 
 
    $client->setAuthConfigFile(CLIENT_SECRET_PATH); 
 
    $client->setAccessType('offline'); 
 

 
    // Load previously authorized credentials from a file. 
 
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
 
    if (file_exists($credentialsPath)) { 
 
    $accessToken = file_get_contents($credentialsPath); 
 
    } else { 
 
    // Request authorization from the user. 
 
    $authUrl = $client->createAuthUrl(); 
 
    printf("Open the following link in your browser:\n%s\n", $authUrl); 
 
    print 'Enter verification code: '; 
 
    $authCode = trim(fgets(STDIN)); 
 

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

 
    // Store the credentials to disk. 
 
    if(!file_exists(dirname($credentialsPath))) { 
 
     mkdir(dirname($credentialsPath), 0700, true); 
 
    } 
 
    file_put_contents($credentialsPath, $accessToken); 
 
    printf("Credentials saved to %s\n", $credentialsPath); 
 
    } 
 
    $client->setAccessToken($accessToken); 
 

 
    // Refresh the token if it's expired. 
 
    if ($client->isAccessTokenExpired()) { 
 
    $client->refreshToken($client->getRefreshToken()); 
 
    file_put_contents($credentialsPath, $client->getAccessToken()); 
 
    } 
 
    return $client; 
 
} 
 

 
/** 
 
* Expands the home directory alias '~' to the full path. 
 
* @param string $path the path to expand. 
 
* @return string the expanded path. 
 
*/ 
 
function expandHomeDirectory($path) { 
 
    $homeDirectory = getenv('HOME'); 
 
    if (empty($homeDirectory)) { 
 
    $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH"); 
 
    } 
 
    return str_replace('~', realpath($homeDirectory), $path); 
 
} 
 

 
// Get the API client and construct the service object. 
 
$client = getClient(); 
 
$service = new Google_Service_Drive($client); 
 

 
// Print the names and IDs for up to 10 files. 
 
$optParams = array(
 
    'pageSize' => 10, 
 
    'fields' => "nextPageToken, files(id, name)" 
 
); 
 
$results = $service->files->listFiles($optParams); 
 

 
if (count($results->getFiles()) == 0) { 
 
    print "No files found.\n"; 
 
} else { 
 
    print "Files:\n"; 
 
    foreach ($results->getFiles() as $file) { 
 
    printf("%s (%s)\n", $file->getName(), $file->getId()); 
 
    } 
 
}

這將顯示10個文件,就像他們說的。

這是(在我看來)最好的方法。

相關問題