Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter YouTube API Library cant do browser upload [GOT IT!]
#1

[eluser]jojo777[/eluser]
Hello everyone,

First of all, english is not my native language, so I hope you can understand me.

I need to upload videos directly to youtube and I found the library that names the topic. Its pretty nice but I do not understand how to do a browser upload to youtube.

I read de documentation here (https://developers.google.com/youtube/ar...er_library) but when I arrive to 'Sending Data to YouTube - Browser based Uploading' I feel lost. I dont know how to send that xml data that the doc says. There is no example and I dont know how to do it.

I would like to know if there is any example because its really frustrating Undecided
I hope someone can help me.

Thanks in advance.
#2

[eluser]Jim Pannell[/eluser]
Hi jojo777

I'm in exactly the same position. If anyone out there has experience of using the YouTube api, we'd be very grateful for some code examples.

Cheers

Jim
#3

[eluser]jojo777[/eluser]
[quote author="Jim Pannell" date="1380970528"]Hi jojo777

I'm in exactly the same position. If anyone out there has experience of using the YouTube api, we'd be very grateful for some code examples.

Cheers

Jim[/quote]


Hi again buddy, finally i got it working. It was a little confusing...but finally it works, I uploaded a video (10MB) directly from my web to my youtube account.

Well i hope you are like me, wondering how the heck I can upload, well here we go:

- First you can successfully grant access to the web of your Youtube account with this code:

Code:
//CALL THIS METHOD FIRST BY GOING TO
//www.your_url.com/index.php/request_youtube
public function request_youtube()
{
  $params['key'] = $this->config->item('google_consumer_key');
     $params['secret'] = $this->config->item('google_consumer_secret');
     $params['algorithm'] = "HMAC-SHA1";

  $this->load->library('google_oauth', $params);
  $data = $this->google_oauth->get_request_token(site_url('video/access_youtube'));
  $this->session->set_userdata('token_secret', $data['token_secret']);
  redirect($data['redirect']);
}

//This method will be redirected to automatically
//once the user approves access of your application
public function access_youtube()
{
  $params['key'] = $this->config->item('google_consumer_key');
     $params['secret'] = $this->config->item('google_consumer_secret');
     $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']);

  redirect(site_url('video/form_youtube'));
}

Its ok? then go further. The code redirects to form_youtube function ok? It will load a simple form with title and/or descripcion for the video (its ok...we'll upload it later..) So:

Code:
public function form_youtube(){
  $this->data['subview'] = 'video/form_youtube';
  if ($this->input->post()) {
         // YOUR VALIDATION RULES.
         // Procesar el formulario.
         if($this->form_validation->run() == TRUE){

    $id_video = $this->video_m->save($data);
    // Guardamos el id insertado en la sesiĆ³n para posteriormente actualizar el registro con el id del video subido a youtube
    $this->session->set_userdata('id_video', $id_video);

    // Preparar envio de metadata a youtube.
    $params['apikey'] = $this->config->item('youtube_api_key');
    $params['oauth']['key'] = $this->config->item('google_consumer_key');
    $params['oauth']['secret'] = $this->config->item('google_consumer_secret');
    $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);

    $metadata = '
     <entry
        xmlns:media="http://search.yahoo.com/mrss/"
        xmlns:yt="http://gdata.youtube.com/schemas/2007">
        <media:group>
          <media:title type="plain">'.$this->input->post('title').'</media:title>
          <media:description type="plain">
            '.$this->input->post('descripcion').'
          </media:description>
          <media:category
            scheme="http://gdata.youtube.com/schemas/2007/categories.cat">YOUR CATEGORY
          </media:category>
          <media:keywords>YOUR, AWESOME, KEYWORDS</media:keywords>
        </media:group>
      </entry>';

    // Recover the response of youtube token
    $response = $this->youtube->getFormUploadToken($metadata);

    // Parse RESPONSE
    $obj = new SimpleXMLElement($response);
    $arr = json_decode(json_encode($obj), TRUE);

    // var_dump($arr);

    // Almacenamos los valores.
    $this->session->set_userdata('url', $arr['url']);
    $this->session->set_userdata('token', $arr['token']);

    redirect('video/youtube_upload');
         }else{
          // Validation FAILS....
         } // Validation

  }
  $this->load->view('_includes/template', $this->data);
  // The view is just a simple form with fields: titulo and descripcion and it will post to this same function ok?
}

If you see we save 2 sessions variables: url and the token, we will use them now in our new function (youtube_upload). This function will load the form that appears in the documentation an we will put de url and the token where URL TOKEN appears in the example. The next url param is the function where youtube will redirect after the response.

Got it??
#4

[eluser]Jim Pannell[/eluser]
Good work! How long does that session that contains the token last though? I'm thinking it'd probably be better to put it in the db. What do you think? Have you progressed any further with this?
#5

[eluser]jojo777[/eluser]
Thanks man I hope it can help all people in our same situation!

Well, as far I know the token and url are valid just for one use. I've not testd it by saving in DB. All the test I made worked perfect, everytime I got a new token and new url.

I played with saving the youtube url with the ID given in the upload and its really cool!! ;-)
#6

[eluser]Jim Pannell[/eluser]
Sounds good. My application (website) is going to be uploading to one specific youtube account via the users of the website. I will need the authentication process to be a one time thing though. That's what I'm working on at the moment.
#7

[eluser]bluepicaso[/eluser]
my code always return false
Code:
if(!$this->youtube->getFormUploadToken($metadata)){
   echo 'nothing';
  }
it always returns NOTHING
the complete code is here
Code:
public function youtube_request()
{
   $params['key'] = $this->config->item('google_consumer_key');
   $params['secret'] = $this->config->item('google_consumer_secret');
   $params['algorithm'] = "HMAC-SHA1";

   $this->load->library('google_oauth', $params);
   $response = $this->google_oauth->get_request_token(site_url("video/youtube_access"));
  
   $this->session->set_userdata('token_secret', $response['token_secret']); //pseudo code for temporarily storing this value

   redirect($response['redirect']);
}

public function youtube_access()
{
  $params['key'] = $this->config->item('google_consumer_key');
  $params['secret'] = $this->config->item('google_consumer_secret');
  $params['algorithm'] = "HMAC-SHA1";    

  $this->load->library('google_oauth', $params);

  $token_secret = $this->session->userdata('token_secret'); //pseudo code for retrieving the stored value

  $oauth = $this->google_oauth->get_access_token(false, $token_secret);
  $this->session->set_userdata('youtube_token', $oauth['oauth_token']); //pseudo code for permanently storing this value
  $this->session->set_userdata('youtube_secret', $oauth['oauth_token_secret']); //pseudo code for permanently storing this value
    
  redirect('video/ytu');
}





/*
  * function ytu
  * @param:
  * desc:
  */

function ytu() {
  $params['apikey'] = $this->config->item('ytApi');
  $params['oauth']['key'] = $this->config->item('ytClientId');
  $params['oauth']['secret'] = $this->config->item('ytClientSecret');
  $params['oauth']['algorithm'] = "HMAC-SHA1";
  $params['oauth']['access_token'] = array('oauth_token'=>urlencode($this->session->userdata('youtube_token')), 'oauth_token_secret'=>urlencode($this->session->userdata('youtube_secret')));
  $this->load->library('youtube', $params);
  $metadata = '&lt;?xml version="1.0"?&gt;
         <entry
          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>
         ';
  if(!$this->youtube->getFormUploadToken($metadata)){
   echo 'nothing';
  }
  
}


EDIT:
woops!! solution to my code was
replacing
Code:
$params['oauth']['key'] = $this->config->item('ytClientId');
  $params['oauth']['secret'] = $this->config->item('ytClientSecret');

by
Code:
$params['oauth']['key'] =  $this->config->item('google_consumer_key');
  $params['oauth']['secret'] = $this->config->item('google_consumer_secret');




Theme © iAndrew 2016 - Forum software by © MyBB