Search code examples
phpyoutube-apizend-gdata

Getting TOKEN_INVALID when uploading to YouTube


I'm attempting to upload a video to YouTube via the API using Zend_Gdata (Zend Framework 1.12.0). I had no problems getting direct upload to work, but browser-based upload always gives me a 400 - INVALID TOKEN error. I'm pretty sure I must be missing something vital but small enough to not notice it.

There are two files involved in this:

index.php

<?php
$youTubeAPIKey = '<API_Key>';
$username = '<user>';
$password = '<pass>';

set_include_path(get_include_path().PATH_SEPARATOR.__DIR__."/vendor");
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

try
{
    $authenticationURL= 'https://www.google.com/accounts/ClientLogin';
    $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
              $username,
              $password,
              $service = 'youtube',
              $client = null,
              $source = 'BrowserUploaderTest', // a short string identifying your application
              $loginToken = null,
              $loginCaptcha = null,
              $authenticationURL);

    $yt = new Zend_Gdata_YouTube($httpClient, "browser upload test", "Test version 0.1", $youTubeAPIKey);
    $videoEntry = new Zend_Gdata_YouTube_VideoEntry();

    $videoEntry->setVideoTitle("Test movie");
    $videoEntry->setVideoDescription("This is a test movie");
    $videoEntry->setVideoPrivate();

    // @todo This must be a valid YouTube category, how to get a list of valid categories?
    $videoEntry->setVideoCategory('Autos');
    $videoEntry->setVideoTags('cars, funny');

    // Get an upload token
    $tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
    $tokenArray = $yt->getFormUploadToken($videoEntry, $tokenHandlerUrl);
    $token = $tokenArray['token'];
    $url = $tokenArray['url'];
    // print "Token value: {$tokenArray['token']}\n url: {$tokenArray['url']}\n";
    $nextUrl = "http://" . $_SERVER['HTTP_HOST'] . "/uploadDone.php";
}
catch (Zend_Gdata_App_HttpException $httpException)
{
    echo $httpException->getRawResponseBody();
}
catch (Zend_Gdata_App_Exception $e) {
    echo $e->getMessage();
}
catch (Exception $e)
{
    print $e->getTraceAsString();
}
?><!DOCTYPE html>
<html>
    <head>
        <title>Testing Youtube upload</title>
    </head>

    <body>
        <table>
            <tr>
                <td>
                    Url:
                </td>
                <td>
                    <?= $url ?>
                </td>
            </tr>
            <tr>
                <td>
                    Token:
                </td>
                <td>
                    <?= $token ?>
                </td>
            </tr>
        </table>
        <form action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>" enctype="multipart/form-data" method="post">
            <input name="token" type="hidden" value="<?= $token ?>" />
            <input name="file" type="file" />
            <input type="submit" value="Upload file" />
        </form>
    </body>
</html>

and uploadDone.php

<?php
print nl2br(print_r($_GET, true));
print nl2br(print_r($_POST, true));

I've searched both on Stack Overflow and spent a couple of hours searching on Google but not found anything that solves it which leads me to believe I'm missing something dead simple. Any help would be appreciated.

A note: This code is only to test the API usage and is taken mostly from Google's Developer's guide (https://developers.google.com/youtube/2.0/developers_guide_php#Browser_based_Upload) and with a little help from the Yii framework documentation (http://www.yiiframework.com/wiki/375/youtube-api-v2-0-browser-based-uploading/). The production code will be rewritten in a more structured manner but that's not important at the moment.


Solution

  • Your action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>" looks suspicious; is that an errant . character in there right after your $url variable gets evaluated, messing up the URL?