Wednesday, 8 May 2013

Linkedin Gem for Rails : How to share dynamic contents to Linkedin from your Rails Application


After Facebook & Twitter share ,its time for Linkedin. I would suggest you to refer my 2 prev posts before going through this article.

Include "linkedin" gem in your Gemfile and bundle .

gem 'linkedin'

Route file:

 match '/linkedin_share/auth'   => 'linkedin_share#auth' , :method => :get , :as => :linkedin_auth

 match '/linkedin_share/callback' => 'linkedin_share#callback' , :method => :get , :as =>  :linkedin_callback


LinkedinShare Controller & Actions : 

class LinkedinShareController < ApplicationController

  def init_client
    
    key = "APP KEY"
    secret = "SECRET KEY"

    linkedin_configuration = { :site => 'https://api.linkedin.com',
        :authorize_path => '/uas/oauth/authenticate',
        :request_token_path =>'/uas/oauth/requestToken?scope=rw_nus',
        :access_token_path => '/uas/oauth/accessToken' }

    @linkedin_client = LinkedIn::Client.new(key, secret,linkedin_configuration )
  end

  def auth
    init_client
  
    cookies["title"] = { :value => "#{params[:title]}", :expires => 1.minute.from_now }
    cookies["url"] = { :value => "#{params[:url]}", :expires => 1.minute.from_now }
  
    request_token = @linkedin_client.request_token(:oauth_callback => "http://#  {request.host_with_port}/linkedin_share/callback")
    session[:rtoken] = request_token.token
    session[:rsecret] = request_token.secret
  
    redirect_to @linkedin_client.request_token.authorize_url
  end

  def callback
    init_client
  
    if session[:atoken].nil?
      pin = params[:oauth_verifier]
      atoken, asecret =  @linkedin_client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
      session[:atoken] = atoken
      session[:asecret] = asecret
    else
      @linkedin_client.authorize_from_access(session[:atoken], session[:asecret])
    end

    c = @linkedin_client
    
    title = "#{cookies["title"]}"
    url = "#{cookies["url"]}"

    c.add_share({
        :comment => title,
        :content => {
          :title => title,
          :submitted_url => url,
          :submitted_image_url => "IMAGE PATH",
          :description => 'YOUR DESCRIPTION'
        }
      })

    redirect_to root_path
  end
end






Twitter gem for Rails : How to share dynamic contents to Twitter from your Rails Application

Before going through this post . I would suggest you to go through my previous FB share  post. You will get to know the basic functionality & working from the prev post .After that please go through Twitter gem description.

I hope you have gone through the prev post and you have got some idea about it.

Now we will start with Twitter share implementation.You have to include  "twitter" & "oauth" gems into your Gemfile.

gem 'twitter'

gem 'oauth'

Compared to FB share , here we need to include "Oauth" gem for authenticating the user to twitter and redirect the user back to your applications Callback action.After getting the Oauth Secret Key i.e returning from Twitter you can configure the Twitter gem settings and you can post to twitter from your Rails app.

Tuesday, 7 May 2013

Fb Graph Gem for Rails : How to Post dynamic contents to Facebook Wall from your Rails Application

This post will introduce the usage of fb_graph Gem with your existing Rails Application.fb_graph has already done a lot for you guys to exploit the FB API features with your Rails Application , you can have an eye on this fb_graph Gem before going through this post.

Suppose you have a blogger kind of application, and when you compose a new post , you would like to share the same in your Facebook wall, and also would like to give the same option for all the users ,those who liked your post and would like to share the same in their Facebook Wall.

I think now almost all the websites are using this feature to increase their user traffic , to popularize their sites,  Since Facebook turned out to be a great option for advertising and marketing your site content.

One day i also felt the same and after one complete days of effort ..... i was successful.            I thought i should share the same for you guys to reduce your one days effort ( or may be less ) for implementing this FB share option.

I hope i am not boring you , anyway stop to all kind of rubbish introduction and lets start coding ..

Thursday, 17 January 2013

Ruby On Rails: Omniauth Devise Authentication using Facebook , Google & Twitter.


This post is just a simple straightforward description of Omniauth:Overview , intended for the beginners , those who wants to try the OmniAuth for the first time with Facebook , Google & Twitter. This post will cover all the basic information i.e From creating the app in Facebook, Google & Twitter for getting the secret key To connect these Apps to your Rails Application.

Things in common :

add Devise & omniauth gem to your Gemfile.

gem 'omniauth' 
gem 'devise' 

First of all install Devise into your application.
rails generate devise:install  
You have to create user model using devise ,
rails g devise user  
and after that we need to add 2 more columns i.e "uid" and 'provider' to our user model.

rails g migration AddColumnsToUsers provider:string uid:string
rake db:migrate
 also dont forget to add ":provider" and ":uid" to your attr_accessible also.

 We have done with the things that is common in authentication using facebook,Google, Twitter.

 We will start with Facebook Authentication first 

 Facebook Authentication :

 add gem 'omniauth-facebook' to your gem file.

 First of all you need to create an application in facebook to get the secret key out .