如何使用Google Drive API托管或流式传输在Google Drive中上传的视频

时间:2019-07-30 19:35:56

标签: php rest api google-drive-api

我正在使用Web应用程序,我需要通过Google Drive API流式传输在Google Drive中上传的视频。我已经创建了服务帐户来上传文件,查看文件并授予权限。而是我授予了在特定域共享文件的权限,我试图按照以下步骤从子域访问文件。这里以我为例,我正在使用w3school.com来访问流视频。
以下是我的代码授予域权限:   **我也曾尝试允许发送电子邮件,然后成功播放视频**

https://developers.google.com/drive/api/v3/manage-sharing

 $domainPermission = new Google_Service_Drive_Permission(array(
        'type' => 'domain',
        'role' => 'reader',
        'domain' => 'w3schools.com'
    ));
    $request = $driveService->permissions->create(
        $fileId, $domainPermission, array('fields' => 'id'));
    $batch->add($request, 'domain');
 $results = $batch->execute();

要使Weblink能够流式传输视频,如下所示::

$service = new Google_Service_Drive($client);

$fileId = 'xxxxxxxxxxxxxxxxxw5e4zLE5mLv9lYob';

$argument = array(
                  "fields"=>"webViewLink",
                  );
  $file = $service->files->get($fileId,  $argument);

  echo "<pre>";
  print_r($file);

it give's webviewlink as follow's 
https://drive.google.com/file/d/xxxxxxxxxxxxxxxxw5e4zLE5mLv9lYob/view?usp=drivesdk

Now to stream video example html code is like this 
<!DOCTYPE html>
<html>
<body>

<video width="320" height="240" controls>
  <source src="https://drive.google.com/file/d/xxxxxxxxxxx5mLv9lYob/view?usp=drivesdk" type="video/mp4">
</video>


</body>
</html>`

我无法在我的子域或主域中播放此视频, 例如:webaccess.w3schools.com或w3schools.com

此过程中可能出现的错误是什么。 请帮助谢谢。

1 个答案:

答案 0 :(得分:1)

我认为您的问题是由于跨域读取阻止(CORB)引起的。因此,在您的情况下,如何使用webContentLink而不是webViewLink?就像下面的URL。

https://drive.google.com/uc?id=xxxxxxxxxxx5mLv9lYob&export=download

修改HTML后,它如下所示。

修改后的HTML:

<!DOCTYPE html>
<html>
<body>

<video width="320" height="240" controls>
  <source src="https://drive.google.com/uc?id=xxxxxxxxxxx5mLv9lYob&export=download" type="video/mp4">
</video>

</body>
</html>

注意:

  • 在这种情况下,必须共享视频文件。请注意这一点。

参考:

如果这不能解决您的问题,我深表歉意。