2013-05-13 46 views
2

定期备份文件到我的谷歌硬盘帐户我有一个文件定期在服务器上更新,我想每天使用php crone作业在我的Google硬盘帐户上备份这个文件。我不想要任何用户身份验证,是否有任何方式只是简单地将我的驱动器用户名和密码与文件一起传递,并将其上传到我的驱动器帐户。因此,只要我访问我的驱动器帐户,就会在那里找到该文件。 或者是否可以在Google云端硬盘服务帐户中上传文件并与我的云端硬盘帐户共享该特定文件。想要在php

请码帮助..在此先感谢

回答

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>"; 
?>