Question

I had a system setup with the YouTube Data API v2 that would let a user upload a video directly to MY YouTube account via a form on my site (I understand the implications of this, please keep in mind that I am just giving a bare bones description of what the tool did). I have so far been unable to figure out how to setup this same system with the v3 API now that v2 is deprecated. Another quick note, I would like to be able to do this without sending the user to the OAuth page since I have no need to access their YouTube account.

Here is a slimmed down version of PHP code that made this work for 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";
    }
?>
Was it helpful?

Solution

I found an excellent article that helped me with this:

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

A couple of things to point out if you use this article to help you with the same problem. The CURL call to http://gdata.youtube.com/action/GetUploadToken needs to be to HTTPS not HTTP and the form action="{{post_url}}?next={{next_url}} needs to be action="{{post_url}}?nexturl={{next_url}}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top