Archive for December, 2013

Connect to Soundcloud API

1. Load Soundcloud Library

 $this->load->library("Services_Soundcloud",$config);

2.We have to specify  Client ID  Secret and Redirect URL sc_connect() method

public function sc_connect()
 {
$config = array(
 'clientId'=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 'clientSecret'=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 'redirecturi'=>"http://127.0.0.1/project/index.php/controller/method"
 );
 $this->load->library("Services_Soundcloud",$config);
$soundcloud = new Services_Soundcloud($config);
$authorizeUrl = $soundcloud->getAuthorizeUrl();
if($authorizeUrl)
 {
 redirect($authorizeUrl);
 }
}

CallBack to Soundcloud API

public function callback()
 {

 $config = array(
 'clientId'=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 'clientSecret'=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
 'redirecturi'=>"http://127.0.0.1/project/index.php/controller/method"
 );

 $this->load->library("Services_Soundcloud",$config);

 $soundcloud = new Services_Soundcloud($config);

 try {
 $accessToken = $soundcloud->accessToken($_GET['code']);
 print_r($accessToken);
 $this->session->set_userdata("access_token",$accessToken);

 } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
 exit($e->getMessage());
 }
 try {
 $me = json_decode($soundcloud->get('me'), true);
 } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
 exit($e->getMessage());
 }

 echo "

";

 print_r($me);
 }

Soundcloud API Library

Download Link : Soundcloud API

Soundcloud Library : Codeigniter  Folder Structure

application/libraries/Soundcloud/Exception.php

application/libraries/Soundcloud/Version.php

application/libraries/Services_soundcloud.php

Creating App

http://soundcloud.com/you/apps

Step 1  : Register Your App

Reg

Step 2 : Form for App Details

Reg2

App Name

URL

Redirect URL (if your are developing Application in localhost use IP Address don’t use localhost)

Save Client ID and Secret

Step 3 : App Created

App

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

YouTube API Library for the CodeIgniter PHP Framework

The files for this project can be found on GitHub at: https://github.com/jimdoescode/CodeIgniter-YouTube-API-Library

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 http://code.google.com/apis/youtube/dashboard/gwt/index.html

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.

api_console_create_project

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

api_console_create_new_project

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.

apis_console_api_key

Create an OAuth 2.0 client ID

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

api_console_api_access_tab

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

api_console_create_oauth2_client_id

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.

api_console_specify_product_name

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.

api_console_client_id_settings

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