Saturday 27 August 2016

Authentication for ebay's seller and use Trading Api for getting seller information

In this blog i am going to describe how to use Trading Api of Ebay for getting seller information.

We will proceed it in two parts -
1. Ebay Authentication.
2. Fetch seller information from Treding Api.

1. Ebay Authentication.

We have to do ebay authentication for verifying the seller. For this we will use omniauth-ebay gem.


So, in Gemfile :-

gem 'omniauth-ebay'

Now add omniauth initializer in config/initializers/omniauth.rb -

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :ebay, "runame", "devid", "appid", "certid", "siteid", "environment", "auth_type"
end


"runame", "devid", "appid", "certid" these details you can find by going into your developer's account at eBay DevZone.

"siteid" - "siteid" represent your country or zone. You can obatain this from ebay.

"environment" - Here you can defined your environment as :production or :sandbox .

"auth_type" - If you want authenticate user every time than you have to define it as 'SignIn' .

After these set up you have to set routes path for ebay oauth callback. So, in routes.rb -

get '/auth/ebay/callback' => 'your_controller#your_method', as: 'ebay_callback'


Now you will be able to access the omniauth session data by accessing

request.env['omniauth.auth']

For token - request.env["omniauth.auth"]['credentials']['token']

2. Fetch seller information from Treding Api.


For getting seller data ebay provides Treding Api. We can use ebay_client gem. it provide lightweight eBay Trading API Client -

In Gemfile :-

 gem 'ebay_client', '~> 0.2.0'

In config/ebay_client.yml :-

development: &sandbox
  api_keys:
    - token: '<YOUR SANDBOX AUTHENTICATION TOKEN>' # we will set token later accourding to User
      devid: '<YOUR SANDBOX DEV ID>'
      appid: '<YOUR SANDBOX APP ID>'
      certid: '<YOUR SANDBOX CERT ID>'

test:
  <<: *sandbox

production:
  api_keys:
    - token: '<YOUR LIVE AUTHENTICATION TOKEN>' # we will set token later accourding to User
      devid: '<YOUR LIVE DEV ID>'
      appid: '<YOUR LIVE APP ID>'
      certid: '<YOUR LIVE CERT ID>'


Now, we have to make user specific yml file so we can set user's token. For this we will generate a Model EbayConfig -

In app/model/ebay_config

require 'yaml'
class EbayConfig < ActiveRecord::Base
  belongs_to :user, inverse_of: :ebay_config

  validates :app_id, :dev_id, :cert_id, :token, presence: true

  ######################### CallBacks##########################
  after_create :create_user_yml

  def self.user_ebay_data(user, user_token) #Pass the value of user as current_user and user_token of request.env["omniauth.auth"]['credentials']['token']
    data = YAML::load_file('config/ebay_client.yml')
    app_id = data["production"]["api_keys"].first["appid"]
    dev_id = data["production"]["api_keys"].first["devid"]
    cert_id = data["production"]["api_keys"].first["certid"]
    token = user_token
    @ebay_config = user.build_ebay_config(app_id: app_id, dev_id: dev_id, cert_id: cert_id, token: token)
    @ebay_config.save
  end

  def create_user_yml
    _user_yml = {"production" => create_user_config_hash}

    _yml_file = File.join(Rails.root, 'ebay_ymls', "#{self.user_id}_ebay_config.yml")
    File.open(_yml_file, "wb") do |file|
      file.write(_user_yml.to_yaml)
    end
  end

  def create_user_config_hash
    {
        api_keys: [{
                       appid: app_id,
                       devid: dev_id,
                       certid: cert_id,
                       token: token
                   }]
    }
  end

end


Now we have to intialize the ebay_client for specific user, For this we will create a module

require 'ebay_client'
module EbayGlue
  class EbayConnector
    attr_accessor :ebay_client

    def initialize(user_id)
      configurations = EbayClient::Configuration.load Rails.root.join('ebay_ymls', "#{user_id}_ebay_config.yml")
      configuration = configurations['production']
      @ebay_client = EbayClient::Api.new configuration
    end

  end
end


Now you can get @ebay_client and it will provide all the information

ex- @ebay_client.get_store.payload
    @ebay_client.get_user



Thats it !!!!!


Friday 19 August 2016

Posting on Facebook Through Ruby On Rails

Here we go to post a link on Facebook with th help of Koala gem.

Koala is a Facebook library for Ruby, supporting the Graph API.

Installation - 

gem "koala", "~> 2.2"

Graph API

The Graph API is the simple, slick new interface to Facebook's data.
Using it with Koala is quite straightforward.  First, you'll need an access token, which you can get through
Facebook's Graph API Explorer (click on 'Get Access Token').

Then, go exploring:

@user = Koala::Facebook::API.new(access_token)


Use that access_token to set this new @user. Now whenever you mention "me" in any Graph API request on a @user, you'll be mentioning this user (@user) as the target for that request. "me" is just the facebook ID for the user itself.

Now you can post to your user's feed.

@user.put_connections("me", "feed", :message => "I am writing on my wall!")

For posting a link -

@user.put_wall_post("message",{link: item_url}, "me").

Thats it.

Cheer Up !!!!

Friday 12 August 2016

ebay Trading API's integration with RoR

For all the information of Seller we have to use Trading API's of ebay.

Here i am going to fetch seller's item with the help of ebay_client gem.

EbayClient

Simple, lightweight eBay Trading API Client.

Installation

Rails

Gemfile:
gem 'ebay_client', '~> 0.2.0'
config/ebay_client.yml:
development: &sandbox
  api_keys:
    - token: '<YOUR SANDBOX AUTHENTICATION TOKEN>'
      devid: '<YOUR SANDBOX DEV ID>'
      appid: '<YOUR SANDBOX APP ID>'
      certid: '<YOUR SANDBOX CERT ID>'

test:
  <<: *sandbox

production:
  api_keys:
    - token: '<YOUR LIVE AUTHENTICATION TOKEN>'
      devid: '<YOUR LIVE DEV ID>'
      appid: '<YOUR LIVE APP ID>'
      certid: '<YOUR LIVE CERT ID>'
You can check it by - 
e.g. rails console:
EbayClient.api.get_ebay_official_time!
# => {:timestamp=>Fri, 22 Nov 2013 12:31:02 +0000}
Now you can get information from below methods - 
EbayClient.api.get_access_token                EbayClient.api.get_constant                    EbayClient.api.get_primary_key
EbayClient.api.get_access_token_info           EbayClient.api.get_encoding                    EbayClient.api.get_print
EbayClient.api.get_all                         EbayClient.api.get_errors                      EbayClient.api.get_proxies
EbayClient.api.get_all_gem_names               EbayClient.api.get_fields                      EbayClient.api.get_proxy
EbayClient.api.get_all_gem_names_and_versions  EbayClient.api.get_first_row                   EbayClient.api.get_proxy_from_env
EbayClient.api.get_all_versions                EbayClient.api.get_first_value                 EbayClient.api.get_question
EbayClient.api.get_and_set                     EbayClient.api.get_key_string                  EbayClient.api.get_queue
EbayClient.api.get_app_access_token            EbayClient.api.get_label                       EbayClient.api.get_relative_path
EbayClient.api.get_app_access_token_info       EbayClient.api.get_labels                      EbayClient.api.get_response
EbayClient.api.get_arguments_from              EbayClient.api.get_length16                    EbayClient.api.get_rr
EbayClient.api.get_attribute                   EbayClient.api.get_name                        EbayClient.api.get_screen_size
EbayClient.api.get_best_compatible             EbayClient.api.get_object                      EbayClient.api.get_string
EbayClient.api.get_best_encoding               EbayClient.api.get_objects                     EbayClient.api.get_string_list
EbayClient.api.get_byte                        EbayClient.api.get_one_gem_name                EbayClient.api.get_time
EbayClient.api.get_bytes                       EbayClient.api.get_one_optional_argument       EbayClient.api.get_token_from_server
EbayClient.api.get_callbacks                   EbayClient.api.get_or_default                  EbayClient.api.get_unpack
EbayClient.api.get_cert_files                  EbayClient.api.get_page                        EbayClient.api.get_user_info_from_cookie
EbayClient.api.get_class                       EbayClient.api.get_page_access_token           EbayClient.api.get_user_info_from_cookies
EbayClient.api.get_comments_for_urls           EbayClient.api.get_parameters                  EbayClient.api.get_user_picture_data
EbayClient.api.get_connection                  EbayClient.api.get_picture                     EbayClient.api.get_value
EbayClient.api.get_connections                 EbayClient.api.get_picture_data

In some of above method you may get error of -
Error - The time range has exceeded.
ex- EbayClient.api.get_seller_list
may through a exception. In this case you have to pass that params in argument.
ex - EbayClient.api.get_seller_list(StartTimeFrom: '2016-07-01T06:38:48.420Z', StartTimeTo: '2016-08-11T06:38:48.420Z')

Friday 5 August 2016

Setup of Ruby On Rails

Hi,

This is my first blog so i am going to start with setup of ruby on rails.

We will be setting up a Ruby on Rails development environment on Ubuntu 15.10 Wily Werewolf.
The reason we're going to be using Ubuntu is because the majority of code you write will run on a Linux server. Ubuntu is one of the easiest Linux distributions to use with lots of documentation so it's a great one to start with.

1. The first step is to install some dependencies for Ruby.

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

2. The installation for rvm is pretty simple:

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.3.1
rvm use 2.3.1 --default
ruby -v
 
3. The last step is to install Bundler 
 
gem install bundler 


git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"
 
The next step is to take the newly generated SSH key and add it to your Github account
 
cat ~/.ssh/id_rsa.pub
 
Once you've done this, you can check and see if it worked:  
 
ssh -T git@github.com
 
You should get a message like this:   
  

Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.
 
 
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodej
 
gem install rails