Please note that the code for this has been moved to GitHub Here
I just want to make a note here, 'huge success'.
I finished work on my youtube API library for CodeIgniter. This will let you connect to youtube and make requests without having to load any Zend GData libraries. It is written exclusively for CodeIgniter.
To use the library you must first pass in some parameters. The apikey is the apikey provided to you once you register with youtube to use the API (I put mine in a config file). Next you want to include the oauth consumer key that you setup when you setup oauth with google. The secret is the consumer secret google provides you if you are using HMAC-SHA1 signing or the file path to your .pem file if you are using RSA-SHA1 signing (again I put these in a config file for easy access/changes). You must then specify the signing algorithm you use to make your oauth signatures. Finally you have to specify the access token and token secret if your user has already authorized your site to connect to youtube on their behalf.
If any of this seems over your head please consult the oauth docs on youtube for further clarification. You can find those here. It should also be noted that this library only does OAuth authentication it does not support AuthSub feel free to add this if you would like though.
This library does require the oauth_helper I wrote for CodeIgniter I will link to the file at the bottom of the page but if you wish to further understand what it does visit the CodeIgniter Wiki here. Also I wrote a library to handle all of the OAuth interactions that is also available on the Wiki here. (I will provide a link to this at the bottom of this post too.)
$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'] = 'RSA-SHA1'; $params['oauth']['access_token'] = array('oauth_token'=>urlencode($token)); $this->load->library('youtube', $params);
That's it you now have a youtube API instance and can make calls. The following methods are available please consult the documentation for usage etc.
Here is a list of methods that are available right out of the box, no authentication needed.
$this->youtube->getVideoEntry($videoId); $this->youtube->getRelatedVideoFeed($videoId, $params = array()); $this->youtube->getVideoResponseFeed($videoId, $params = array()); $this->youtube->getKeywordVideoFeed(array $keywords, $params = array()); $this->youtube->getVideoCommentFeed($videoId, $params = array()); $this->youtube->getTopRatedVideoFeed($params = array()); $this->youtube->getMostViewedVideoFeed($params = array()); $this->youtube->getRecentlyFeaturedVideoFeed($params = array()); $this->youtube->getWatchOnMobileVideoFeed($params = array());
The list of methods below requires an authenticated user before they can be used.
$this->youtube->getUserPlaylistFeed($user = 'default', $params = array()); $this->youtube->getPlaylistFeed($playlistid, $params = array()); $this->youtube->getSubscriptionFeed($user = 'default', $params = array()); $this->youtube->getContactFeed($user = 'default', $params = array()); $this->youtube->getUserUploads($user = 'default', $params = array()); $this->youtube->getUserFavorites($user = 'default', $params = array()); $this->youtube->getUserProfile($user = 'default'); $this->youtube->getUserActivity($user = 'default'); $this->youtube->getInboxFeedForCurrentUser();
Finally these methods all require some type of data to be specified whether that be XML meta data, a comment or a boolean value.
$this->youtube->directUpload($path, $contenttype, $metadata, $user = 'default'); $this->youtube->getFormUploadToken($metadata); $this->youtube->addComment($videoId, $comment, $commentId = false); $this->youtube->addVideoResponse($videoId, $responseId); $this->youtube->addNumericRating($videoId, $rating); $this->youtube->addLikeDislike($videoId, $like);
All methods where the user is defined as 'default', or are getting data from the current user, and the form upload token method, all require oauth authentication or they wont work. The methods that let you define the user may work without authentication but will only show data that the specified user has made public. The form upload token method requires xml meta data to be passed in as well. You can see what this should look like here. All API request return XML and it is your job to parse it and get what you want from it.
I should note that I haven't tested all of the methods defined in this library so I cannot say with 100% certainty that they all work, but from the ones I have tested I can make a pretty good assumption that they do. Also I used RSA-SHA1 signing for my OAuth requests I just couldn't get HMAC to work properly I found an issue with my signing method for HMAC and fixed it but I cannot guarantee that it works properly either. If you have an issue post it in the comments and give me your HTTP header information for both your request and the response. There is a DEBUG constant flag you can set that will output this information to your error logs. I am also going to post a more technical brief of this on the CodeIgniter wiki which I will link to here.
[EDIT] There seems to be some confusion about how all this stuff fits together so let me clarify the steps. Please Please Please read all the documentation before leaving a comment.
- Download the linked files and put them in the correct CI directories. You need all 3 of them. Two go in the application/libraries folder and one goes in the application/helpers folder.
- Read the documentation on OAuth. Having a basic understanding of how it works is key. Make sure you read the oauth_helper and google_oauth wiki pages. Both contain vital information about generating the necessary keys and how to use those files. Read the oauth_helper wiki first. It has links on generating some keys and how to use them. Don't worry too much about understanding how oauth_helper works it is more for internal purposes but you do need to checkout the links in its wiki. Read the google_oauth wiki next as it provides more working examples of what you need to do to get oauth up and running and get the necessary access token.
- Make your controller to get a request and access token. Samples are provided on the google_oauth wiki page. If you have read the documentation up to this point then it shouldn't be too difficult to figure out what to do. Just use the google_oauth wiki controller examples for reference.
- Test your controller methods out and make sure you are getting an access token and there are no hiccups in the 3 step process. You can get an access token as many times as you need so don't worry if you messed something up and need to get a new one.
- Read the youtube api wiki page and check out the official youtube api documentation which can be found here
- Make your controller to access the youtube api. You will pass in the access token you got from the previous steps as well as an API key you get from google. Make sure you read up on all of the methods the api provides. It is by no means a complete API library there are a number of missing features which may get added in the future but my hope is that you can figure out how to add the things you need by reading the official youtube api documentation and looking at how I implemented things in the api library. Really what it boils down to is HTTP header manipulation.
- Finally put everything together. The process should be pretty seamless with the user only having to leave your site for a split second to approve the request token. Also note that you can do this via a local server so even if you setup everything to your sites domain if you are running a dev instance locally everything should still work.
If you have read everything on this blog post and all the subsequent pages it links to and you still have questions please let me know. Judging from some of the questions I have had all of the info is in the blog post and the linked pages I think the problem is: there isn't a clear order to it all. For that I apologize. Follow the steps outlined and it should make things a bit easier.