【发布时间】:2021-04-13 18:41:36
【问题描述】:
我正在尝试使用 php 脚本将 zip 文件上传到谷歌驱动器,作为 Fedora 33 x86-64 上的 cron 作业。我已经尝试了几种不同的方法,但目前我对此没有任何运气。我已经创建了一个服务帐户并下载了凭据,创建了 oauth concent 屏幕,安装了 Google Drive API、oauth 客户端 ID 等……无论如何,当我从 shell 运行脚本时,我感到很奇怪。任何帮助将不胜感激。
<?php
require_once 'vendor/autoload.php';
$googleClient = new Google_Client();
$googleClient->setAuthConfig('***************************.json');
$googleClient->addScope(Google_Service_Drive::DRIVE);
$googleClient->setApplicationName('******************');
$googleClient->setAccessType('offline');
$googleClient->getAccessToken();
$drive = new Google_Service_Drive($googleClient);
$file = new Google_Service_Drive_DriveFile($googleClient);
$file->setName('Mysql-Backup.zip');
$file->setMimeType('application/zip');
try {
$return = $drive->files->create($file, array('data' => file_get_contents("Mysql-Backup.zip"), 'uploadType' => 'multipart', 'supportsAllDrives' => 'true'));
echo "Sucessful upload of " . $file['name'] . ".";
}
catch (Exception $exc) {
echo $exc->getMessage() . "\n";
}
?>
如果我在stackoverflow.com 上的代码和发布都没有正确执行此操作,请原谅我。好的,我已按照您的建议列出文件,您确实是正确的,文件已上传到我的服务帐户而不是我的个人驱动器。我还尝试了您使用 Web 界面将文件夹从我的个人帐户共享到服务帐户的建议。但是,当我再次尝试时,它仍然无法正常工作。这给了我一个错误“PHP 致命错误:未捕获的错误:调用 /scripts/google-drive/google-drive.php:25 中未定义的方法 Google_Service_Drive_Resource_Permissions::insert()”。我参考了https://developers.google.com/resources/api-libraries/documentation/drive/v3/php/latest/class-Google_Service_Drive_Permission.html 和https://developers.google.com/drive/api/v2/reference/permissions/insert#php,但我仍然不明白我错过了什么。我不知道这是否令人不悦,但如果他们有一个带有说明的黑醋栗工作 php 类,我愿意支付宝某人。这方面的文档到处都是旧的东西不起作用而新的东西不起作用。在这一点上,我正在努力。
<?php
require_once 'vendor/autoload.php';
$googleClient = new Google_Client();
$googleClient->setAuthConfig('****');
$googleClient->addScope("https://www.googleapis.com/auth/drive.file");
$googleClient->setApplicationName('MySQL-Backup');
$googleClient->setAccessType('offline');
$googleClient->getAccessToken();
$drive = new Google_Service_Drive($googleClient);
$file = new Google_Service_Drive_DriveFile($googleClient);
$file->setName('Mysql-Backup.zip');
$file->setMimeType('application/zip');
$file->setParents('****');
try
{
$return = $drive->files->create($file, array('data' => file_get_contents("Mysql-Backup.zip"), 'uploadType' => 'multipart', 'supportsAllDrives' => 'true'));
echo "Sucessful upload of " . $file['name'] . " File ID:" . $return->id . "\n";
$newPermission = new Google_Service_Drive_Permission();
//$newPermission->setValue('***');
$newPermission->setType('user');
$newPermission->setRole('editor');
echo $return->id . "\n";
try
{
$drive->permissions->insert($return->id, $newPermission);
}
catch (Exception $exc)
{
echo 'Caught exception: ', $exc->getMessage(), "\n";
}
}
catch (Exception $exc)
{
echo 'Caught exception: ', $exc->getMessage(), "\n";
}
$optParams = array('fields' => '*');
$myfiles = $drive->files->ListFiles($optParams);
foreach( $myfiles as $k => $file )
{
echo "{$file['name']} - {$file['id']} ---- " . $file['mimeType'] . "\n";
try
{
// subfiles
$sub_files = $drive->files->listFiles(array('q' => "'{$file['id']}' in parents"));
foreach( $sub_files as $kk => $sub_file )
{
echo "{$sub_file['name']} - {$sub_file['id']} ---- " . $sub_file['mimeType'] . "\n";
}
}
catch (Exception $exc)
{
echo 'Caught exception: ', $exc->getMessage(), "\n";
}
}
?>
【问题讨论】:
-
它说“成功上传...”。你有什么问题?
-
@TangentiallyPerpendicular 我怀疑他们真正的问题是他们的文件上传的“位置”。
-
@FooFOO 不发布消息或代码的图像。请发布更易于阅读的实际文本。
标签: php google-api google-drive-api google-api-php-client service-accounts