2013-05-13 3 views
2

저는 정기적으로 서버에서 업데이트되는 파일을 가지고 있으며,이 파일을 php crone 작업을 사용하여 Google 드라이브 계정에서 매일 백업하려고합니다. 난 어떤 사용자 인증 싶지 않아, 거기에 그냥 파일과 함께 내 드라이브 사용자 이름과 비밀 번호를 전달하고 내 드라이브 account.So에 업로드 할 때마다 내 드라이브 계정에 액세스 할 때마다 거기에 그 파일을 발견 어떤 방법입니다. 또는 Google 드라이브 서비스 계정에 파일을 업로드하고 특정 파일을 드라이브 계정과 공유 할 수있는 경우 파일을 Google 드라이브 계정에 정기적으로 백업하고 싶습니다.

내가 구글 드라이브 문서에서 내 질문에 대한 대답을 얻었다 사전

답변

4

에 .. 코드 감사를 도와주세요 ... 구글 드라이브 계정 중 하나에 서비스 계정에서 업로드 된 파일을 공유 할 수 있습니다. 해당 파일을 볼 수있는 드라이브 계정에 대한 특정 파일에 대한 사용 권한을 제공해야합니다. 내 코드는 다음과 같습니다.

<?php 
require_once "google-api-php-client/src/Google_Client.php"; 
require_once "google-api-php-client/src/contrib/Google_DriveService.php"; 
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php"; 
session_start(); 

function buildService() { 

$DRIVE_SCOPE = array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', 'https://gdata.youtube.com/action/GetUploadToken', 'https://gdata.youtube.com'); 
$SERVICE_ACCOUNT_EMAIL = '[email protected]'; 

if($_SERVER['HTTP_HOST'] == 'localhost') 
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'YOUR_CERTIFICATE-privatekey.p12'; 
else 
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = $_SERVER['DOCUMENT_ROOT'].'YOUR_CERTIFICATE-privatekey.p12'; 

    $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH); 
    $auth = new Google_AssertionCredentials(
     $SERVICE_ACCOUNT_EMAIL, 
     $DRIVE_SCOPE, 
     $key); 

    $client = new Google_Client(); 
    $client->setApplicationName("You Tube API"); 
    $client->setUseObjects(true); 
    $client->setAssertionCredentials($auth); 
    $client::$auth->refreshTokenWithAssertion(); 
    $json = $client->getAccessToken(); 
    $accessToken = json_decode($json)->access_token; 

    return new Google_DriveService($client); 
} 
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) { 

    $file = new Google_DriveFile(); 
    $file->setTitle($title); 
    $file->setDescription($description); 
    $file->setMimeType($mimeType); 

    try { 
    $data = file_get_contents($filename); 

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

    // Uncomment the following line to print the File ID 
    //print 'File ID: %s'.$createdFile->getId(); 
    //print 'Parent ID<pre>'.print_r($createdFile->getParents())."</pre>"; 

    $fileId = $createdFile->getId(); 
    $file = $service->files->get($fileId); 

    $newPermission = new Google_Permission(); 
    $newPermission->setValue('[email protected]'); 
    $newPermission->setType('user'); 
    $newPermission->setRole('reader'); 
    try { 
     return $service->permissions->insert($createdFile->getId(), $newPermission); 
    } catch (Exception $e) { 
     print "An error occurred: " . $e->getMessage(); 
    } 


    return $createdFile; 
    } catch (Exception $e) { 
    print "An error occurred: " . $e->getMessage(); 
    } 
} 


$service = buildService(); 

$createdFile=insertFile($service, 'Testing', 'Testing for file upload', 'Parent_Id_', 'application/pdf', 'test.pdf'); 
echo "<pre>".print_r($createdFile)."</pre>"; 
?> 
관련 문제