使用api获取Google drive video embed代码

时间:2017-11-27 10:49:10

标签: php video iframe google-drive-api embed

大家好,

我正在开展一个项目,我需要在网页上专门显示来自我的谷歌硬盘的视频。我不想手动选择视频并复制其嵌入代码,有没有办法我们可以使用google api(首选PHP)获取视频的嵌入代码?

这样我就可以在iframe中使用此代码并显示视频。

如果还有其他方法,请另外建议。

提前致谢,

1 个答案:

答案 0 :(得分:0)

您可以从Google Drive API https://developers.google.com/drive/v3/web/quickstart/php开始,我希望这段代码可以为您提供帮助。

<?php 
// New Google Client, see all settings at link in description
$client = new Google_Client();

// New Google Drive Service
$service = new Google_Service_Drive($client);

// Params with filter of MIME type for search only videos mp4 (you can change this)
$optParams = [
  'q' => "mimeType='video/mp4'",
  'pageSize' => 10,
  'fields' => 'nextPageToken, files(id, name, webViewLink)'
]; 

// Get files from our request
$files = $service->files->listFiles($optParams);

// Print an iframe for each video
foreach($files->files as $file){
    // Now I need to make a little detail about the next lines of code so look at [Info]
    $src = str_replace('/view', '/preview', $file->webViewLink);
    echo '<iframe width="500" height="200" target="_parent" src="'. $src .'"></iframe>'
}
?>

<强> [信息]

$service->files->listFiles($optParams)返回的对象是Google_Service_Drive_DriveFile个对象,每个对象都有一系列属性。从Google API的v3开始,名为embedLink的属性已被删除,并且v2(Migrate to Google Drive API v3)无法替代此属性,因此您在我的代码中看到我使用的webViewLink属性返回这样的文件视图的URL:

https://drive.google.com/file/d/123456789/view

但是如果您在<iframe>内使用此网址,则浏览器会向您发出错误消息:

Refused to display 'https://drive.google.com/file/d/123456789/view' in a frame because it set 'X-Frame-Options' to 'sameorigin'.` 

我没有在这个问题上抱太多关于此事的问题,而lot of question就是这个问题。所以我们需要请求预览文件,而不是视图,我用

来做

str_replace('/view', '/preview', $file->webViewLink);

请求返回的URL。现在,您可以在<iframe>

中使用此网址
相关问题