Friday, July 27, 2012

API accesses

Its great how far technology has advanced in terms of providing access. Now-a-days one gets all the APIs to access any online application. They even document it so precisely and accurately. And if one wants a wrapper for languages like Ruby, Python, Perl and PHP then that is readily available as well.

Well I'm going to shortly discuss about some Gems I have used to access these APIs.

The first Gem that I purely awesome is the 'omniauth' Gem. It has all the routines necessary to build add-ons for other Omniauth Plugins. You might need to read more about oauth to get an idea. I managed to use the plugins - omniauth-twitteromniauth-facebookomniauth-linkedin and omniauth-instagram with this Gem. And its so easy.

You will have to add an omniauth.rb file in the initializers for starters.


Rails.application.config.middleware.use OmniAuth::Builder do 
 provider :linkedin, LINKEDIN_CONFIG['api_key'], LINKEDIN_CONFIG['secret_key']   
 provider :twitter, TWITTER_CONFIG['consumer_key'], TWITTER_CONFIG['consumer_secret'] 
 provider :facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret'], :scope => FACEBOOK_CONFIG['scope'], :display => 'popup' 
 provider :instagram, INSTAGRAM_CONFIG['client_id'], INSTAGRAM_CONFIG['client_secret'], :scope => INSTAGRAM_CONFIG['scope'] 
end


It might be a good idea to insert these configuration files on deployment, rather than put them in the git.

The you will have to call something like

current_user.services.create!(
 :provider => auth['provider'], 
 :uid => auth['uid'], 
 :token => (auth['credentials']['token'] rescue nil), 
 :secret => (auth['credentials']['secret'] rescue nil))

Better add in lots of Exception handling.

This is the authorization part. The idea is generally at the end of the authentication, you will get a token which you should use for further requests to these services. To access the data from these services, one would have to use other Gems and then pass the auth_token (and auth_secret).

The gems used to access the data are :
fb_graph Gem for Facebook
twitter Gem for Twitter
linkedin Gem for Linked In
instagram Gem for Instagram

Try them out. You will love them.

No comments: