使用api获取Google云端硬盘视频嵌入代码 [英] Get Google drive video embed code using api
问题描述
大家好,
我正在研究一个项目,我需要在网页上专门显示来自Google驱动器的视频。我不想手动选择视频并复制其嵌入代码,是否可以通过Google api(首选PHP)获取视频的嵌入代码?
I was working on a project where i need to display a video specifically from my google drive on a webpage. I don't want to manually select the video and copy its embed code, is there a way we can obtain the embed code for a video using google api (PHP preferred) ?
以便我可以在iframe中使用此代码并显示视频。
So that i can use this code within an iframe and display the video.
请也建议是否还有其他方法。
Please also suggest if there are other ways.
谢谢,
推荐答案
您可以从Google Drive API开始 https://developers.google.com/drive/v3 / web / quickstart / php 和这段代码希望对您有所帮助。
You can start with Google Drive API https://developers.google.com/drive/v3/web/quickstart/php and this segment of code i hope can help you.
<?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的第3版中,删除了名为 embedLink
的属性,并且第2版中的该属性没有其他选择(迁移到Google Drive API v3 ),如您在我的代码中所见,我使用了webViewLink属性,该属性将URL返回到这样的文件视图:
The objects returned from $service->files->listFiles($optParams)
are Google_Service_Drive_DriveFile objects and each of these have a series of properties. From v3 of Google API a properties called embedLink
was removed and there isn't an alternative for this property from v2(Migrate to Google Drive API v3) so as you seen in my code i use webViewLink property that return a URL to a view of file like this:
https://drive.google.com/file/d/123456789/view
但是,如果您在< iframe>
中使用此URL您出错:
But if you use this URL inside an <iframe>
the browser notice to you an error:
Refused to display 'https://drive.google.com/file/d/123456789/view' in a frame because it set 'X-Frame-Options' to 'sameorigin'.`
I,因为它会将 X-Frame-Options设置为 sameorigin。我在这个问题上并没有让您抱太多,对此有一个很多问题。因此,我们需要请求文件的预览,而不是视图,我需要使用
I'm not holding you too much on this issue and there are a lot of question about this. So we need to request a preview of file and not a view an i do that with
str_replace('/ view','/ preview' ,$ file-> webViewLink);
请求返回的网址。现在,您可以在< iframe>
on the URL returned by the request. Now you can use this URL in a <iframe>
这篇关于使用api获取Google云端硬盘视频嵌入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!