YouTube API v2.0 – Browser-based Uploading

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

Through the YouTube API you can upload files directly to the Youtube server. Youtube currently offers two services 1. Direct Link 2. Browser Based Uploading. In this wiki I will demonstrate how you can quickly set Browser Based Uploading for your Yii application. This is the protocol request that takes place.

Process Flow

However, Before you read further please note that this functionality will not work on a localhost. You need your application live so that Google can finish the protocol request.

  1. You would need to download the Zend Gdata framework the current stable version is 1.12.0. http://framework.zend.com/download/gdata/

  2. Extract the contents to /protected/vendors/ . You would need to create the vendors folder. The folder structure should look like /protected/vendors/Zend .

  3. Once that is you would need to create a youtube account and generate a developer key. The youtube account can be created on youtube.com (if you don't have a valid gmail account). I personally prefer a new account for my applications.

  4. In your main controller file create the following function. We do this in order to call the video upload function explicitly.

public function actionVideo()
	{
    	
          $this->render('video');
		 		
	}
  1. In your protected/views/current-controller name folder create a video.php file and copy the contents of the code below to the file.
<?php

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

     $authenticationURL = 'https://www.google.com/accounts/ClientLogin';

     $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
                      $username          = 'Your youtube id here without @gmail.com',
                      $password           = 'Your password',
                      $service           = 'youtube',
                      $client           = null,
                      $source           = 'Your application name',
                      $loginToken           = null,
                      $loginCaptcha      = null,
                      $authenticationURL);

     $devkey = 'Your very long developer key here';

          $yt = new Zend_Gdata_YouTube($httpClient, '', '', $devkey);
          $video = new Zend_Gdata_YouTube_VideoEntry();
          
		 
          $video->setVideoTitle('Your video title);
          $video->setVideoDescription('Description of the video');
          $video->setVideoPrivate();
          $video->setVideoCategory('Select a category'); // see Youtube. Else you may get an error. Avoid using People & Blogs. People alone or Blogs alone is good.
          $video->SetVideoTags('apps');
          $handler_url     = 'http://gdata.youtube.com/action/GetUploadToken';
          $token_array     = $yt->getFormUploadToken($video, $handler_url);
          $token          = $token_array['token'];
          $post_url     = $token_array['url'];
          $next_url      = 'http://yourdomain.com/yourappname/index.php/controller/action';
?>

<form action="<?php echo $post_url ?>?nexturl=<?php echo $next_url ?>"
method="post" enctype="multipart/form-data">
     <input name="file" type="file"/>
     <input name="token" type="hidden" value="<?php echo $token ?>"/>
     <input value="Upload Video File" type="submit" />
</form>

So how does it work. When you call the url yourdomain.com/yourappname/index.php/controller/video

A request is sent to youtube for fileupload by sending certain parameters like Video Title, Category etc.. Youtube returns request with your token (temporary). The user is displayed the upload video field. Once the User submits the video. Youtube returns the video ID and status message to the url you set in the $next_url. For example.

http://yourdomain.com/appname/index.php/controller/action?status=200&id=_hgfGF7637

This ID would be your youtube video ID which you can store in your database.

You can also watch this in action here at http://youtu.be/qszLqC3aSeo

2 0
7 followers
Viewed: 16 787 times
Version: Unknown (update)
Category: How-tos
Written by: Vaibhav
Last updated by: Vaibhav
Created on: Aug 30, 2012
Last updated: 5 years ago
Update Article

Revisions

View all history

Related Articles