TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Invalid_Token of Youtube API + Kohana
  • I'm a newbie to Kohana framework. I want to create a webpage to upload Video into my personal Youtube Channel using Youtube API V 2.0 with Curl PHP. I have customized some codes from the original code (http://rd2inc.com/blog/2012/08/youtube-browser-based-uploader-with-php/). If I tested with pure PHP, I can successfully uploaded the video file but when I combine this code with Kohana framework, I got one error "Error code 400, Status: INVALID_TOKEN". It related to incorrect authorization access. I have googling and tried many ways but not success.

    Could anyone share me some ideas to fix this problem? Thanks.

  • @novice Post your code pal.

  • @Jack Ellis, Thanks for your reply. Here are the codes here: Note: I got this codes from http://rd2inc.com/blog/2012/08/youtube-browser-based-uploader-with-php/. I've just customized it into Controller and View structure.

    Controller/VideoUpload.php

    class Controller_VideoUpload extends Controller_Application {

        public function action_add_video() {
        $content = View::factory('pages/add_video');
         if (isset($_POST['addvideo'])){
            $youtube_token = $this -> action_youtube_token();
            $urls = $youtube_token->url;
            $tokens= $youtube_token->token;
            $content -> set_global('urls', $urls);
            $content -> set_global('tokens', $tokens);
        }
        $this -> template -> content = $content;
    }
    public function action_youtube_token(){
        $youtube_email = "my_youtube_chanel Email";
        $youtube_password = "my_youtube_channel password"; 
            $key = "my_developer_key"; 
            $source = 'anytext'; // A short string that identifies your application for logging purposes.
            $postdata = "Email=".$youtube_email."&Passwd=".$youtube_password."&service=youtube&source=" . $source;
        $curl = curl_init( "https://www.google.com/youtube/accounts/ClientLogin" );
        curl_setopt( $curl, CURLOPT_HEADER, "Content-Type:application/x-www-form-urlencoded" );
        curl_setopt( $curl, CURLOPT_POST, 1 );
        curl_setopt( $curl, CURLOPT_POSTFIELDS, $postdata );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
    
        //getting the Auth number
        $response = curl_exec( $curl );
        curl_close( $curl );
    
        $auth_array = explode( "\n", $response );
        list( $auth, $youtubeuser ) = $auth_array;
    
        $auth_equal = explode( "=", $auth);
        $auth_trim = array_map("trim", $auth_equal);
        list($authlabel, $authvalue) = $auth_trim;
    
        $youtube_user = explode( "=", $youtubeuser);
    
        if ($youtube_user[0] != ""){
            echo "youtube_user not null";
            list( $youtubeuserlabel, $youtubeuservalue ) = array_map( "trim", $youtube_user );
        }
    
        $youtube_video_title = "new video";
        $youtube_video_description = "video upload"; 
        $youtube_video_keywords = "Test"; 
        $youtube_video_category = "Entertainment";  
    
        $data = '<?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">' . stripslashes( $youtube_video_title ) . '</media:title>
                    <media:description type="plain">' . stripslashes( $youtube_video_description ) . '</media:description>
                    <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtube_video_category.'</media:category>
                    <media:keywords>'.$youtube_video_keywords.'</media:keywords>
                  </media:group>
                </entry>';
    
        $headers = array( "Authorization: GoogleLogin auth=".$authvalue,
                 "GData-Version: 2",
                 "X-GData-Key: key=".$key,
                 "Content-length: ".strlen( $data ),
                 "Content-Type: application/atom+xml; charset=UTF-8" );
    
        $curl1 = curl_init( "http://gdata.youtube.com/action/GetUploadToken");
    
        curl_setopt( $curl1, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
        curl_setopt( $curl1, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $curl1, CURLOPT_TIMEOUT, 10 );
        curl_setopt( $curl1, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $curl1, CURLOPT_POST, 1 );
        curl_setopt( $curl1, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt( $curl1, CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $curl1, CURLOPT_POSTFIELDS, $data );
        curl_setopt( $curl1, CURLOPT_REFERER, true );
        curl_setopt( $curl1, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt( $curl1, CURLOPT_HEADER, 0 );
    
        $response1 = simplexml_load_string( curl_exec( $curl1 ) );
        curl_close( $curl1 );
    
        return $response1; 
    
    }
    

    }

    views/pages/youtube_upload.php

    if (isset($_POST['addvideo'])){

        $nexturl = "http://localhost/myhomepage/";      
        if (isset($_GET['id']) && isset($_GET['status']))
        {
            $unique_id = $_GET['id'];
            $status = $_GET['status'];
    
        }   
        if ($tokens !=''){
               echo "<form action=" . $urls . "?nexturl=" . urlencode($nexturl) 
            ." method='post' enctype='multipart/form-data'>";
               echo "<label> Upload Video</label><br>
            <input id='file' type='file' name='videofile'/>
            <input type='hidden' name='token' value=" . $tokens . "/>
                 <input type='submit' value='Upload Video' name='videoupload' class='pure-button-success'>
            </form>";
        }
        if(isset($unique_id) != '' && $status == '200' )
        {
            echo "successfully uploaded";
                 echo "the video link = 'http://www.youtube.com/watch?v=". $unique_id . "'"; 
        }
    

    }

    I wonder whether we have to configure some additional config file in Kohana to make it fully authorization access Youtube API access token?

    Thanks

  • @novice, I suggest you search for an OAuth 2.0 client module for Kohana and your code would be cleaner. Usually, we handle external request with Kohana's Request class and not curl. Perhaps you find a Youtube module for Kohana, if you do a little search...

  • @enov, Thanks very much for your comment. I'll try to search about it.

  • Dear ALL,

    I have found this module (https://github.com/pocesar/googleapi-kohana) which include "YOUTUBE API" with "my_upload" file. I think this is what I want to find. I don't really understand much how to combine it with my project yet even though I have read the project's readme file which doesn't explain in detail how to use this project. I want to contact the owner, @Mr. Paulo Cesar, to ask for more detail. unfortunately, he hasn't left any Email or contactable information in Github.

    Notice that I have never use any framework before. After searching and comparing the advantages and disadvantages of frameworks, I decided to use Kohana as my first framework. I've done self-study just around 1 month, so my knowledge about kohana is still limited.

    Could anyone give me some tip about this module (https://github.com/pocesar/googleapi-kohana) ?
    Shall I copy whole folder of googleapi-kohana into "application/vendor" or into "modules/"?

    Thanks in advance for all your help.

  • Notice that I have never use any framework before. After searching and comparing the advantages and disadvantages of frameworks, I decided to use Kohana as my first framework.

    Mostly, you made a wise decision, except for that part that it's your first framework ;-)

    Shall I copy whole folder of googleapi-kohana into "application/vendor" or into "modules/"?

    Copy the folder into modules. Enable the module in the bootstrap.php.

  • @enov, Thanks a lot for your reply. I have done what you have recommended. :-) Now M trying to go through the codes so I can customize it to what I need.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion