【问题标题】:File upload to google drive with service account authentication successful but not missing服务帐户身份验证成功但未丢失的文件上传到谷歌驱动器
【发布时间】: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.htmlhttps://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


【解决方案1】:

您的消息表明它已上传。我怀疑如果你做一个file.list

$optParams = array(
  'fields' => '*'
);
$service->files->ListFiles($optParams);

你会发现文件实际上已经上传了。

您的问题是文件的上传位置。您的问题应该是“我的文件上传到哪里了?”

您需要记住,服务帐户是虚拟用户,他们拥有自己的 Google Drive 帐户。您已将文件上传到服务帐户驱动器帐户。您查看上传文件的唯一方法是通过执行我在上面建议的 file.list 以编程方式。

尝试通过网络界面与服务帐户电子邮件地址共享您个人云端硬盘帐户上的目录,就像您与任何其他用户共享一样。然后,当您在驱动器帐户中上传文件集父母 = 文件夹 ID 时。该文件将上传到您的帐户,而不是服务帐户拥有的帐户。

只需记住在上传文件后设置文件的权限,因为服务帐户上传了文件,它将是文件的所有者。

【讨论】:

  • 我已经按照您的建议列出了文件,您确实是正确的,文件已上传到我的服务帐户而不是我的个人驱动器。我还尝试了您使用 Web 界面将文件夹从我的个人帐户共享到服务帐户的建议。但是,当我再次尝试时,它仍然无法正常工作。
  • 您是否将父文件夹设置为您与服务帐户共享的个人帐户上的文件夹?您需要设置父目录或者它只是继续将文件上传到服务帐户根目录。
  • 是的,我试过 $file->setParents('****');使用我与服务帐户共享的个人驱动器上的文件夹的 folderid。但是,当我尝试使用 $drive->permissions->insert($return->id, $newPermission); 设置权限时PHP 抛出一个错误。我已经更新了原始消息底部的代码。
【解决方案2】:

我建议你关注这个https://packagist.org/packages/miqoo1996/google-drive

示例中有所有功能,可以在您的平台上轻松实现 Google Drive。

非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2022-07-25
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    相关资源
    最近更新 更多