【问题标题】:Want to take a regular backups of files to my google drive account in php想要在 php 中定期将文件备份到我的谷歌驱动器帐户
【发布时间】:2013-05-13 12:53:05
【问题描述】:

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

请帮助编写代码.. 提前致谢

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    我从 google drive 文档中得到了我的问题的答案...可以将从服务帐户上传的文件共享到任何 google drive 帐户。只是我们必须为我们必须查看该文件的驱动器帐户提供该特定文件的权限....我的代码如下:

    <?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 = 'SOMETHING@developer.gserviceaccount.com';
    
    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('DRIVE_ACCOUNT_TO_SHARE@gmail.com');
        $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>";
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多