YouTube Data API v3直接表单上传

时间:2014-07-29 17:48:50

标签: youtube youtube-api gdata youtube-data-api

我有一个使用YouTube Data API v2的系统设置,可以让用户通过我网站上的表单直接将视频上传到我的 YouTube帐户(我明白其含义,请保留记住,我只是简单地描述了该工具的用途。到目前为止,我还没有弄清楚如何使用v3 API设置这个相同的系统,因为v2已被弃用。另一个快速说明,我希望能够在不将用户发送到OAuth页面的情况下执行此操作,因为我无需访问他们的 YouTube帐户。

这是一个精简版的PHP代码,使其适用于v2:

<?php
    if ($_GET['op'] == "yt") {
        if ($_GET['status'] == "200") {
            // video upload was successful, $_GET['id'] contains the YouTube video ID for the upload
        } else {
            // video upload failed
        }
    } else {
        require_once("Zend/Loader.php");

        Zend_Loader::loadClass("Zend_Gdata_ClientLogin");
        Zend_Loader::loadClass("Zend_Gdata_YouTube");

        $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
            $username       = "YOUTUBE_USERNAME",
            $password       = "YOUTUBE_PASSWORD",
            $service        = "youtube",
            $client         = null,
            $source         = "My Video Upload Bla Bla Bla",
            $loginToken     = null,
            $loginCaptcha   = null,
            "https://www.google.com/accounts/ClientLogin"
        );

        $yt = new Zend_Gdata_YouTube($httpClient, "My Video Upload Bla Bla Bla", "My Video Upload Bla Bla Bla", "YOUTUBE_DEVELOPER_KEY");
        $yt->setMajorProtocolVersion(2);

        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
        $myVideoEntry->setVideoTitle("Video Title");
        $myVideoEntry->setVideoDescription("Video Description");
        $myVideoEntry->setVideoCategory("People");

        $accessControlElement = new Zend_Gdata_App_Extension_Element("yt:accessControl", "yt", "http://gdata.youtube.com/schemas/2007", "");
        $accessControlElement->setExtensionAttributes(array(
            array("namespaceUri" => "", "name" => "action", "value" => "list"),
            array("namespaceUri" => "", "name" => "permission", "value" => "denied")
        ));

        $myVideoEntry->setExtensionElements(array($accessControlElement));

        $tokenArray = $yt->getFormUploadToken($myVideoEntry, "http://gdata.youtube.com/action/GetUploadToken");

        $nextUrl = urlencode("http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?op=yt");

        print "<form action=\"{$tokenArray['url']}?nexturl={$nextUrl}\" method=\"POST\" enctype=\"multipart/form-data\">\n";
        print " <input name=\"file\" type=\"file\" />\n";
        print " <input name=\"token\" type=\"hidden\" value=\"{$tokenArray['token']}\" />\n";
        print " <input value=\"Upload Video File\" type=\"submit\" />\n";
        print "</form>\n";
    }
?>

1 个答案:

答案 0 :(得分:0)

我发现了一篇很好的文章,帮助我解决了这个问题:

http://jasonwritescode.blogspot.com/2013/09/youtube-browser-based-uploading-with.html

如果您使用本文来帮助您解决同样的问题,请指出几点。对http://gdata.youtube.com/action/GetUploadToken的CURL调用需要是 HTTPS 而不是HTTP,而表单操作=&#34; {{post_url}}?next = {{next_url}}需要是action = &#34; {{POST_URL}}的 nexturl = {{next_url}}