我需要使用youtube API v3将视频从我的网站用户上传到他的YouTube帐户。
我正在研究Symfony。我让youtube api在本地工作,但我直接把视频文件的路径放在代码中,如下所示:
$videoPath = "C:\Users\Doodou\Desktop\mavideo.mp4";
现在,我想让用户可以在他的计算机上选择一个视频,将其放在他的YouTube帐户上。我知道如何进行视频上传。
我想知道如何在文件上传和youtube api之间建立链接?我是否必须使用临时路径放入$ videoPath或者我必须将文件下载到我的网站以及之后的youtube吗?
以下是youtube API的有趣部分:
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
try{
// REPLACE this value with the path to the file you are uploading.
$videoPath = "/path/to/file.mp4";
// Create a snippet with title, description, tags and category ID
// Create an asset resource and set its snippet metadata and type.
// This example sets the video's title, description, keyword tags, and
// video category.
$snippet = new Google_Service_YouTube_VideoSnippet();
$snippet->setTitle("Test title");
$snippet->setDescription("Test description");
$snippet->setTags(array("tag1", "tag2"));
// Numeric video category. See
// https://developers.google.com/youtube/v3/docs/videoCategories/list
$snippet->setCategoryId("22");
// Set the video's status to "public". Valid statuses are "public",
// "private" and "unlisted".
$status = new Google_Service_YouTube_VideoStatus();
$status->privacyStatus = "public";
// Associate the snippet and status objects with a new video resource.
$video = new Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
// Specify the size of each chunk of data, in bytes. Set a higher value for
// reliable connection as fewer chunks lead to faster uploads. Set a lower
// value for better recovery on less reliable connections.
$chunkSizeBytes = 1 * 1024 * 1024;
// Setting the defer flag to true tells the client to return a request which can be called
// with ->execute(); instead of making the API call immediately.
$client->setDefer(true);
// Create a request for the API's videos.insert method to create and upload the video.
$insertRequest = $youtube->videos->insert("status,snippet", $video);
// Create a MediaFileUpload object for resumable uploads.
$media = new Google_Http_MediaFileUpload(
$client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($videoPath));
// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
编辑:我快要做到了。我找到了如何使用临时路径并将其用于上面代码中的路径。最后一个问题是,用户首次上传视频然后允许我访问他的YouTube帐户时出现错误:
Error calling PUT https://www.googleapis.com/upload/youtube /v3/videos?part=status%2Csnippet&uploadType=resumable&upload_id=AEnB2UrB5y55Fj4ZaPR3RIIuVe5PXx8jxsk3XfQWYsBbnOnjR7u6klvd7japoYfuPwjgSupHE5JE9W9N7o_MqEKes4gYpkNKOA: (400) Invalid Upload Request
如果我回到我的上传页面并进行新的上传,则出现此错误后,它正在运行!
我认为问题来自于第一次上传时,上传开始,然后是youtube的连接/授权,然后回到上传。我不知道这是一个问题,因为温度路径或因为youtube ...如果你有任何想法:)
对不起我的英文:)