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 !!!!!
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 !!!!!
No comments:
Post a Comment