我目前在做一個Web應用程序PHP腳本放棄我的硬盤: 通過在瀏覽器中打開一個PHP,我有一個基於所提供的示例腳本Google(https://github.com/google/google-api-php-client)。谷歌驅動器帳戶列表上運行一個文件列表編程
這個腳本只是進行到文件列表和文件複製到服務器上。 它工作正常,但我希望我的雲端硬盤文件與我的服務器文件同步。每次我想再次複製我的文件時,我必須在瀏覽器中運行我的PHP。這裏是我的有關問題:
- 我可以做這將被執行編程由cron每天一個PHP腳本? 目前在我的瀏覽器中打開的PHP需要身份驗證(如果未檢測到Google帳戶的cookie已檢測到,我首先需要登錄)。我想在自己的帳戶上授權我的Google App,以便每天都可以取消該驅動器。
- 我可以在多個Google帳戶上應用此腳本嗎(例如有G地址或令牌列表,並且腳本爲每個帳戶運行)。我正在做的Web應用程序是爲了一些人,我想將他們的驅動器文件與我的服務器同步。
感謝您事先的任何答覆。我不知道這是否非常有用,但這裏是我的代碼:
function retrieveAllFiles($service, $parameters) {
$results = array();
$pageToken = NULL;
do{
try {
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$results = array_merge($results, $files->getFiles());
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "Une erreur s'est produite : " . $e->getMessage();
$pageToken = NULL;
}
}while($pageToken);
return $results;
}
// Function to get a list of files id
function scrapDrive($service, $excluded) {
$final_files = array();
$query = "";
if(sizeof($excluded) == 0){
$query = "name = '--------'";
}else{
$query = "name = '".$excluded[0]."'";
}
// I just do a custom query if the user has chosen to exclude personal folders from his drive
foreach ($excluded as $folder_name) {
$query .= " or name ='".$folder_name."'";
}
// Get folders ID
$excluded_folders = retrieveAllFiles($service, array('q' => $query));
$excluded_folders_id = array();
foreach ($excluded_folders as $folder) {
array_push($excluded_folders_id, $folder['id']);
}
$usermail = getUserMail($service);
// Getting only files from last month
$artworks_query = "modifiedTime > '".date("Y-m-d\TH:i:sP",strtotime("-1 month"))."'";
// Only getting images
$artworks_query .= " and mimeType contains 'image/' and '".$usermail."' in owners";
foreach ($excluded_folders_id as $id) {
$artworks_query .= " and not '".$id."' in parents";
}
$artworks = retrieveAllFiles($service, array('q' => $artworks_query));
foreach ($artworks as $artwork) {
$final_artworks[$artwork['id']] = $artwork['name'];
}
return $final_artworks;
}
$excluded = getExcludedFolders($_SESSION['id']);
$artworks = scrapDrive($service, $excluded);
$nb_of_artworks = sizeof($artworks);
$i = 1;
// Copy each file
foreach ($artworks as $artwork_id => $artwork_name) {
$response = $service->files->get($artwork_id, array(
'alt' => 'media'));
$content = $response->getBody()->getContents();
file_put_contents("folder/".$artwork_id.'_'.$artwork_name, $content);
}
$i++;
}
在此先感謝您的幫助! :-)
請參閱https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention-canonic – pinoyyid
您好,感謝您的回覆。我已經看過這篇文章,這是關於從oauth操場上做的,而不是通過腳本來做的,對嗎? – saperlipopette
閱讀完整的文章。它僅使用oauth操場來獲取刷新令牌。然後,將該令牌嵌入到腳本中。 – pinoyyid