使用 api 获取 Google 驾驶视频嵌入代码
Get Google drive video embed code using api
大家好,
我正在做一个项目,我需要在网页上显示专门来自我的 google 驱动器的视频。我不想手动 select 视频并复制其嵌入代码,有没有一种方法可以使用 google api (PHP首选)?
这样我就可以在 iframe 中使用此代码并显示视频。
如果有其他方法也请指教
提前致谢,
你可以从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)
编辑的对象 return 是 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) 所以正如你在我的代码中看到的,我使用 webViewLink 属性 那 return a URL 到这样的文件视图:
https://drive.google.com/file/d/123456789/view
但是如果你在 <iframe>
中使用这个 URL 浏览器会通知你一个错误:
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 return 上按请求编辑。现在您可以在 <iframe>
中使用此 URL
大家好,
我正在做一个项目,我需要在网页上显示专门来自我的 google 驱动器的视频。我不想手动 select 视频并复制其嵌入代码,有没有一种方法可以使用 google api (PHP首选)?
这样我就可以在 iframe 中使用此代码并显示视频。
如果有其他方法也请指教
提前致谢,
你可以从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)
编辑的对象 return 是 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) 所以正如你在我的代码中看到的,我使用 webViewLink 属性 那 return a URL 到这样的文件视图:
https://drive.google.com/file/d/123456789/view
但是如果你在 <iframe>
中使用这个 URL 浏览器会通知你一个错误:
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 return 上按请求编辑。现在您可以在 <iframe>