Sunday 23 October 2016

Google authentication with 'signet' ruby gem

We will implement google authentication with 'signet' gem. It is official ruby gem of google. It support  OAuth 1.0 / OAuth 2.0 implementation.

So, in Gemfile.


  gem 'signet'

run bundle.

Now, we have to create a google application on google developer console. This will provide us app_id, client_id and client_secret.

After that we will initialize the client of google auth for particular user.

In User.rb

  def initialize_google_client
    client = Signet::OAuth2::Client.new(
        :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
        :token_credential_uri =>  'https://www.googleapis.com/oauth2/v3/token',
        :client_id => 'Your client Id is here',
        :client_secret => 'Your client secret is here',
        :scope => 'email profile',
        :redirect_uri => 'redirect url same as you have put in your google     application'
    )
  end


In above initializer you can add scope as per your requirement.

Now, we will create routes for authentication path and callback redirect path.

In routes.rb

  get '/google_auth'=> 'some_controller#google_auth', as: 'google_authentication'
  get 'google_callback' => 'some_controller#google_callback'

Now, in controller -

  def google_auth
    client = current_user.initialize_google_client
    redirect_to(client.authorization_uri)
  end

  def google_callback
    client = current_user.initialize_google_client
    client.code = request['code']
    token = client.fetch_access_token!
  end


Above method provide you, access_token, refresh_token and id_token for particular user.


Saturday 8 October 2016

Twitter authentication and Posting url and message on Twitter through RoR

In this blog, we will implement authentication with twitter and posting on twitter.

First, we will implement twitter authentication with omniauth-twitter.

So, in Gemfile -



gem 'omniauth-twitter'

After that in config/intializer/omniauth.b -


Rails.application.config.middleware.use OmniAuth::Builder do
# get consumer_key and consumer_secret from creating a developer account in twitter
    provider :twitter, ['consumer_key'], ['consumer_secret'], {:display => "popup",:x_auth_access_type => 'Read, Write & Direct Messages'}

end



In routes.rb -




# Here we are using oauths controller. Set callback url in twitter developer account as https://localhost:3000/auth/twitter/callback
get '/auth/twitter/callback' => 'oauths#twitter_callback', as: 'twitter-callback'


Now in controller -


  def twitter_callback
    token = request.env['omniauth.auth']['extra']['access_token'].params[:oauth_token]
    token_secret = request.env['omniauth.auth']['extra']['access_token'].params[:oauth_token_secret]
    twitter_id = request.env['omniauth.auth']['extra']['access_token'].params[:user_id]
  end


Above method return token, token_secret and twitter_id of twitter user.

Now, we have all done with twitter authentication.

Now for posting we will use twitter gem.

In Gemfile -

gem 'twitter', '~> 5.16'

We have to initialize the twitter cleint. Below method intialize
In model - # you can put anywhere initializer method as per your convenience.


def initialize_twitter_client
    data = YAML::load_file(File.join(Rails.root, 'config', 'api_keys.yml'))[Rails.env]
    client = Twitter::REST::Client.new do |config|
      config.consumer_key = ['consumer_key'] # put here consumer_key of twitter app.
      config.consumer_secret = ['consumer_secret'] # put here consumer_secret of twitter app.
      config.access_token = ['token'] # We have alraedy got token from twitter authentication for perticular twitter user.
      config.access_token_secret = ['token_secret'] # It is also provided by twitter authentication for perticular twitter user.
    end
end


Now, we can post on twitter from below method



def post_to_twitter(product)
    client = initialize_twitter_client
    message = "Put your message here. Be sure it will not more than 140 character"[0..139]
    client.update("#{message} #{url}")
end

Sunday 2 October 2016

Posting on pinterest through RoR

In last blog we have implemented pinterest authentication. Now, we will implement posting on pinterest.

2.) We will do post on pinterest with pinterest-api gem 

So, in Gemfile -


gem 'pinterest-api'

run bundle

From last blog we can get token from pinterest_callback with the help of this method we can initialize the client for pinterest api-

In Model pinterest.rb -


def initialize_pinterest_client
  @client = Pinterest::Client.new(token) #Get token from pinterest authentication.
end

For posting on pintesert we have to create pinterest board.


def create_pinterset_board(board_name)
  @client = initialize_pinterest_client
  board_id = @client.create_board({name: board_name}).data.id
end

Now, we can create pin on pinterest -


def post_to_pinterest(product)
  @client = initialize_pinterest_client
  @client.create_pin({board: board_id, note: post_name, link: url, image_url: image_url})
end