Question

I want to upload videos with next way:

  1. I just upload file to server (as usual)
  2. My server-side Yii-application takes that video and uploads it on Youtube from a special account on youTube

What do i have:

  • My YouTube (google) account name and email. "name" or "name@gmail.com"
  • My password
  • A developer Key, which I found in Google's "Product Dashboard"
  • A name of the application, which names 'myapp':

Product Dashboard: myapp

So, I read some docs in google and decided that best way for me is to use ClientLogin auth type, because I have only one account to use and I have all necessary data. I found an example for ZendFramework's GData and I imported it into my Yii application.

I specially simplified the code just to upload one single video from /upload directory to test that it works. I expect to find a video in my YT account uploaded. Of course there is no video and here I am :-) Complete code of the action is below:

Yii::import('application.vendors.*');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

$yt_user = 'myYTname';
$yt_pass = 'myYTpass';
$yt_source = 'myapp';
$yt_api_key = 'veryVERYlongKEYhere';

$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
    $username = $yt_user,
    $password = $yt_pass,
    $service = 'youtube',
    $client = null,
    $source = $yt_source,
    $loginToken = null,
    $loginCaptcha = null,
    $authenticationURL
);
$yt = new Zend_Gdata_YouTube($httpClient, $yt_source, null, $yt_api_key);
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$filesource = $yt->newMediaFileSource(Yii::getpathOfAlias('webroot').'/upload/videos/video.mp4');
$filesource->setContentType('video/mp4');
$filesource->setSlug('video.mp4');
$myVideoEntry->setMediaSource($filesource);
$myVideoEntry->setVideoTitle('My Test Movie');
$myVideoEntry->setVideoDescription('My Test Movie description');
$myVideoEntry->setVideoCategory('Autos');
$myVideoEntry->SetVideoTags('cars, funny');
$myVideoEntry->setVideoDeveloperTags(array('mydevtag', 'anotherdevtag'));

$uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/{$yt_user}/uploads";
try {
    $newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');
} catch (Zend_Gdata_App_HttpException $httpException) {
    echo $httpException->getRawResponseBody();
} catch (Zend_Gdata_App_Exception $e) {
    echo $e->getMessage();
}

As you can see, there is a lot of default code from the official example. But it doesn't work. Noone echo shows me information. But when I deleted try-catch, I got an error:

Zend_Gdata_App_HttpException
Read timed out after 10 seconds 
Was it helpful?

Solution

So, this problem is solved by myself :) First of all: don't try to upload from localhost! Then in my case I got an error, that I didn't say my dev-key! So, if you got the same error, try to change this:

    $newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');

by adding the 4th parameter - extra headers:

$yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry', array(
    'X-GData-Key' => 'key=yourBIGbigBIGdeveloperKEYhere'
));

Good luck and have fun with youtube API!

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