6. Youtube API – Browse Based Upload

Posted: 24/12/2013 in Youtube API
Tags: , , ,

Step – 1 : Upload Video Metadata

  • ACCESS_TOKEN
  • DEVELOPER_KEY
  • CONTENT_LENGTH
  • API_XML_Request

HTML Form

<form action=”<?=base_url()?>index.php/controller/method” method=”post” >
<table style=”margin: 0 auto”>
<tr><td>Title : </td><td><input type=”text” name=”title” /></td></tr>
<tr><td>Description : </td><td><textarea name=”description”></textarea> </td></tr>
<tr><td></td><td><input type=”submit” value=”Upload”/></td></tr>
</table>
</form>

POST /action/GetUploadToken HTTP/1.1
Host: gdata.youtube.com
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Content-Length: 1941255
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <media:group>
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    </media:description>
    <media:category
      scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
    </media:category>
    <media:keywords>toast, wedding</media:keywords>
  </media:group>
</entry>

Step 2 – Extracting values from the API response

When you submit an Upload API request, the API returns an XML response that contains an upload URL and upload token that enable the user to upload the actual video file. You will need to extract the URL and token from the response and include these values in the form on your web page where the user submits the video file.

The example below shows a sample API response to a request for browser-based uploading:

<?xml version='1.0' encoding='UTF-8'?>
<response>
  <url>http://uploads.gdata.youtube.com/action/FormDataUpload/AEF3087AUD<url>
  <token>AEwbFAQEvf3xox...</token>
</response>

Step 3 – Uploading the video file

After extracting the upload URL and upload token from the API response, you need to display a form so that the user can upload the actual video file. The form must use the upload URL as the value of the <form> tag’s action attribute and have a hidden input field containing the upload token. In addition, the form should verify that the user has selected a file to upload before allowing the user to actually submit the form.

<script type="text/javascript">
  function checkForFile() {
    if (document.getElementById('file').value) {
      return true;
    }
    document.getElementById('errMsg').style.display = '';
    return false;
  }
</script>

<form action="URL?nexturl=http%3A%2F%2Fwww.example.com" method="post"
  enctype="multipart/form-data" onsubmit="return checkForFile();">
  <input id="file" type="file" name="file"/>
  <div id="errMsg" style="display:none;color:red">
    You need to specify a file.
  </div>
  <input type="hidden" name="token" value="TOKEN"/>
  <input type="submit" value="go" />
</form>

Step 4 – Codeigniter Method For Upload Video

public function browse_based_upload()
{
$params['apikey'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$params['oauth']['key'] = 'xxxxxxxxxxxx.apps.googleusercontent.com';
$params['oauth']['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$params['oauth']['algorithm'] = 'HMAC-SHA1';
$params['oauth']['access_token'] = array('oauth_token'=>urlencode($this->session->userdata('oauth_token')),
'oauth_token_secret'=>urlencode($this->session->userdata('oauth_token_secret')));
$this->load->library('youtube', $params);
$title = $this->input->post('title'); // Video Title
$description = $this->input->post('description'); // Video Description
$privacy_setting = "public";
$this->session->set_userdata('video_title',$title);
$this->session->set_userdata('video_description',$description);
$this->session->set_userdata('video_privacy_setting',$privacy_setting);
var_dump($privacy_setting); // Privacy Setting
if($privacy_setting == "public") // Public
{
$metadata = '
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">'.$title.'</media:title>
<media:description type="plain">'.$description.'</media:description>
<media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>
</media:group>
</entry>';
$data1 = $this->youtube->getFormUploadToken($metadata);
$obj = new SimpleXMLElement($data1);
$arr = json_decode(json_encode($obj), TRUE);
$url=$arr['url'];
$token=$arr['token'];
$ut['urltoken'] = array('url'=>$url,'token'=>$token);
$this->load->view('redirect_viewname',$ut);
}
else if($privacy_setting == "private")
{
 $metadata =
 '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:gd="http://schemas.google.com/g/2005" gd:fields="media:group/yt:private">
 <media:group>
 <media:title type="plain">'.$this->input->post('title').'</media:title>
 <media:description type="plain">'.$this->input->post('description').'</media:description>
 <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People</media:category>
 <yt:private/>
 </media:group>
 </entry>';
$data1 = $this->youtube->getFormUploadToken($metadata);
$obj = new SimpleXMLElement($data1);
$arr = json_decode(json_encode($obj), TRUE);
$url=$arr['url'];
$token=$arr['token'];
$ut['urltoken'] = array('url'=>$url,'token'=>$token);
$this->load->view('redirect_viewname',$ut); } }

Step 5 – Important Steps

1. Need to add nexturl
2. You must set the value of the <form> tag's enctype attribute to multipart/form-data.
3. The <input> tag that identifies the file must be named file.
4. The <input> tag that contains the token must be named token.
5. Your application should verify that the user has selected a file to upload before allowing the user to submit the form to upload the file.

Step 6 – Process Flow Diagram

yt_browser_upload_flow

Step 7 – Process Flow Diagram

After Uploading Video , Return you will get Status and Video_id
1. Status = 200
2. Video_id = xxxxxxxxxxx

Refereed By : Google API’s

Advertisements
Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s