可恢复上传到谷歌驱动器,但服务帐户错误

问题描述 投票:0回答:1

当前尝试使用 PHP 向 google Drive api 进行可恢复上传,但出现错误“预期类型 'Psr\Http\Message\RequestInterface'。找到 'Google\Service\Drive\DriveFile'”。这是我的代码:

$client = new \Google\Client();
 $client->setAuthConfig('credentials.json');
 $driveService = new Google\Service\Drive($client);
 $file = new Google\Service\Drive\DriveFile();
 $file->setName($filename);
 $chunkSizeBytes = 1 * 1024 * 1024;
 $client->setDefer(true);
 $request = $driveService->files->create(
  $file,
  [
  'data' => file_get_contents($filepath),
  'uploadType' => 'resumable'
  ]
 );



// ERROR
 $chunkSizeBytes = 100 * 1024 * 1024;
 $media = new Google\Http\MediaFileUpload(
  $client,
  $request,
  'text/csv',
  null,
  true,
  $chunkSizeBytes
 );

错误是在 MediaFileUpload 中传递 $request 时发生的。 谢谢

php file-upload google-drive-api
1个回答
2
投票

从你的例子来看:

$driveService = new Google\Service\Drive($client);
// [...]
$client->setDefer(true);
// [...]

在我看来,

$client->setDefer(true)
设置得太晚了。也在谷歌的例子中。在我看来,因此这条线总是错误的。如果为true,则返回应该是RequestInterface。

因此我会尝试以下方法。我没有 GoogleDrive,这就是为什么我无法测试它。

$client->setDefer(true);
$driveService = new Google\Service\Drive($client);

更新:

我昨天已经对此感兴趣,并且昨天再次尝试了一些东西。

在以下示例中,$request 来自类型 RequestInterface。昨天我还尝试在 Google 上生成身份验证数据。但不知何故,身份验证错误不断出现。我确信我错过了一些东西。当我设置

$client->setDefer(false);
时出现验证错误。

$client = new Client();
$client->setAuthConfig(__DIR__ . "/oauth-credentials.json");
$client->setDefer(true);
$driveService = new Google\Service\Drive($client);
$file = new DriveFile();
$file->setName('test.txt');

$request = $driveService->files->create(
    $file,
    [
        'data' => file_get_contents(__DIR__ . "/test.txt"),
        'uploadType' => 'resumable'
    ]
);

$this->assertInstanceOf(RequestInterface::class, $request);

我认为,这是 GoogleAPI 包中的问题。这个例子已经是错误的。该错误也会显示在 IDE 中。并非 Google 所做的一切都是完美的。

大文件上传.php

这是 2023 年 3 月 Stackoverflow 上的讨论:
使用服务帐户上传到谷歌驱动器生成错误

2019 年 7 月的一张未解决的票证似乎已被关闭但未得到解决:
https://github.com/googleapis/google-api-php-client/issues/1680

或者这个未解决的问题:
https://github.com/googleapis/google-api-php-client/issues/1881

© www.soinside.com 2019 - 2024. All rights reserved.