Google drive api 想用php定期备份文件到我的google drive帐户吗

Google drive api 想用php定期备份文件到我的google drive帐户吗,google-drive-api,Google Drive Api,我有一个在服务器上定期更新的文件,我想每天使用php crone作业在我的Google drive帐户上备份这个文件。我不需要任何用户身份验证,是否有任何方式,我只需将我的驱动器用户名和密码与文件一起传递,并将其上载到我的驱动器帐户。因此,每当我访问我的驱动器帐户时,我都会在那里找到该文件。 或者,是否有可能上传谷歌硬盘服务帐户中的文件,并与我的硬盘帐户共享该特定文件 请在代码方面提供帮助。。提前感谢我从google drive文档中获得了问题的答案…可以将从服务帐户上载的文件共享到任何goog

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


请在代码方面提供帮助。。提前感谢

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

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

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