Archive for the ‘Youtube API’ Category

Step – 1 : Upload Video Metadata

  • API_XML_Request


<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>

POST /action/GetUploadToken HTTP/1.1
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=""
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    <media:keywords>toast, wedding</media:keywords>

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'?>

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;

<form action="URL?" 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.
  <input type="hidden" name="token" value="TOKEN"/>
  <input type="submit" value="go" />

Step 4 – Codeigniter Method For Upload Video

public function browse_based_upload()
$params['apikey'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$params['oauth']['key'] = '';
$params['oauth']['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$params['oauth']['algorithm'] = 'HMAC-SHA1';
$params['oauth']['access_token'] = array('oauth_token'=>urlencode($this->session->userdata('oauth_token')),
$this->load->library('youtube', $params);
$title = $this->input->post('title'); // Video Title
$description = $this->input->post('description'); // Video Description
$privacy_setting = "public";
var_dump($privacy_setting); // Privacy Setting
if($privacy_setting == "public") // Public
$metadata = '
<entry xmlns="" xmlns:media="" xmlns:yt="">
<media:title type="plain">'.$title.'</media:title>
<media:description type="plain">'.$description.'</media:description>
<media:category scheme="">People</media:category>
$data1 = $this->youtube->getFormUploadToken($metadata);
$obj = new SimpleXMLElement($data1);
$arr = json_decode(json_encode($obj), TRUE);
$ut['urltoken'] = array('url'=>$url,'token'=>$token);
else if($privacy_setting == "private")
 $metadata =
 '<entry xmlns="" xmlns:media="" xmlns:yt="" xmlns:gd="" gd:fields="media:group/yt:private">
 <media:title type="plain">'.$this->input->post('title').'</media:title>
 <media:description type="plain">'.$this->input->post('description').'</media:description>
 <media:category scheme="">People</media:category>
$data1 = $this->youtube->getFormUploadToken($metadata);
$obj = new SimpleXMLElement($data1);
$arr = json_decode(json_encode($obj), TRUE);
$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


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


YouTube API Library for the CodeIgniter PHP Framework

The files for this project can be found on GitHub at:

Folder Structure

  • 1. google_oauth.php – project/application/libraries.
    This library retrieves an OAuth access token to authenticate with YouTube
  • 2. Youtube.php – project/application/libraries.
    This library interacts with the API to submit your requests and retrieve XML responses to those requests
  • 3. Oauth_helper.php – project/application/helpers.
    It signs and builds OAuth HTTP headers and is used internally by both library files.

2. Get an API key to access YouTube’s API. To obtain a key, go to

3. Establish OAuth Authentication from your new domain.

4. Configure Youtube API Key in Codeigniter

  • Step 1 : project/application/config.config.php
  • Step 2 : $config[‘youtube_api_key’] = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’;

Refereed By : Google API’s

You need to register your application with Google to be able to use OAuth 2.0 authorization. The following steps explain how to register your application and create an OAuth 2.0 client ID to use in that application.

Create your project and select API services

Create a project in the Google APIs Console.

1. If you have not created a project before, the Console page displays a big Create project button.


2. If you have created a project before, you can create a new project via the Console menu shown below.


3. The console displays the Services pane, which shows a list of the Google API services that your application could use. Toggle the status for the YouTube Data API v3 service to ON. (You may need to read and accept a license agreement to complete this step for certain services.)

4. After enabling the services that your application will use, you can click on the API Access pane to retrieve your API key. API requests must specify an API key or an OAuth 2.0 token.


Create an OAuth 2.0 client ID

  1. Click the API Access pane in the navigation menu.


2. Click the Create an OAuth 2.0 client ID button.


3. Enter a product name for your application and, if you wish, a URL for your product logo. By default, we will use your email address as the support email for your application. You can change the email address by signing in to the API Console with a different account that has an email address that you have verified.


Click the Next button to proceed

4. Indicate whether your application is a web application or an installed application. If it is a web application, you will need to also provide a domain or hostname.


5. After you direct a user to Google’s authorization server, and the user logs in and decides whether to grant access to your application, Google will redirect the user to a redirect_uri that you specified.

  1. For web applications, you need to register the redirect_uri that your application uses with Google before you will be able to complete the steps needed to obtain OAuth 2.0 access tokens. You can register more than one redirect_uri.The Console will define a default redirect URI and a default, authorized JavaScript source. You can register additional redirect_uri values and add or change authorized JavaScript sources by clicking the more options link in the Client ID Settings window.
  2. Click the Create client ID button.

Refereed By : Google API’s

This process involves uploading a video stored on your server to YouTube.

The metadata field is the API_XML_Request as defined in the YouTube API documentation and the contenttype is the MIME-type of the video to upload and Video URL.

$videoPath = 'D:\BlackBerry120.3gp';
$videoType= "video/3gpp";
$params['apikey'] = 'API-Key';
$params['oauth']['key'] = ''; // Key
$params['oauth']['secret'] = 'xxxxxxxxxxx'; // Secret
$params['oauth']['algorithm'] = 'HMAC-SHA1';
$params['oauth']['access_token'] = array('oauth_token'=>urlencode($this->session->userdata('oauth_token')),
$this->load->library('youtube', $params); $metadata = '<entry xmlns="" xmlns:media="" xmlns:yt=""> <media:group> <media:title type="plain">Test Direct Upload</media:title> <media:description type="plain">Test Direct Uploading.</media:description> <media:category scheme="">People</media:category> <media:keywords>test</media:keywords> </media:group> </entry>'; echo $this->youtube->directUpload($videoPath, $videoType, $metadata);

Refereed By : Google API’s

Developer Registration

Generating Access Token

1.  (Developer Key-Google)

Create Project


2.  API Project -> Services -> Add Services (Youtube, Google Plus, Picasa, Google Maps)

3. Youtube (YouTube Data API v3 and YouTube Analytics API )

4. API Access

  • Create Oauth 2.0 Client ID
  • Client ID Setting
    1. Web Application
    2. Site URL (htp://localhost of
    3. Redirection URL
  • Generated Client ID and Secret
  • Client ID:
    Email address:
    Client secret: xxxxxxxxxxxx
    Redirect URIs: http://localhost/projectname
    JavaScript origins: http://localhost


Refereed By : Google API’s

1. Request to Youtube using API

public function request_youtube()
 $params['key'] =';
 $params['secret'] = 'xxxxxxxxxxxx';
 $params['algorithm'] = 'HMAC-SHA1';
$this->load->library('google_oauth', $params);
 $data = $this->google_oauth->get_request_token(base_url().'index.php/example/access_youtube');
 $this->session->set_userdata('token_secret', $data['token_secret']);

2. Access

public function access_youtube()
 $params['key'] =';
 $params['secret'] = 'xxxxxxxxxxxx';
 $params['algorithm'] = 'HMAC-SHA1';
$this->load->library('google_oauth', $params);
$oauth = $this->google_oauth->get_access_token(false, $this->session->userdata('token_secret'));
$this->session->set_userdata('oauth_token', $oauth['oauth_token']);
 $this->session->set_userdata('oauth_token_secret', $oauth['oauth_token_secret']);

Refereed By : Google API’s