Project

taobao-top

0.01
No commit activity in last 3 years
No release in over 3 years
a simple ruby implementation of TOP API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

< 4, >= 3.2
~> 2.0
~> 1.1
~> 1.6.0
 Project Readme

Taobao::Top

A simple ruby implementation of Taobao TOP API 淘宝开放平台

Installation

Add this line to your application's Gemfile:

gem 'taobao-top'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install taobao-top

Usage

Configuration

For Rails, add following lines in 'config/initializers/taobao_top.rb':

Taobao::TOP.sandbox = !Rails.env.production?
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if Taobao::TOP.sandbox?

Invoke a API method:

require 'taobao/top'

Init a service instance by following lines:

service = Taobao::TOP::Service.new(APP_KEY, APP_SECRET, :session => "SESSION")

Or

service = Taobao::TOP::Service.new(APP_KEY, APP_SECRET) do |options|
  options.session = "SESSION"
  options.format = "json"
  options.sign_method = "md5"
end

then invoke a API method:

response = service.get("taobao.users.get", :nick => "test")
unless response.error?
  user = response.body.user
  p user.nick # => test
else
  p response.error.code
  p response.error.msg
  p response.error.sub_code
  p response.error.sub_msg
end

Multipart content

service = Taobao::TOP::Service.new(APP_KEY, APP_SECRET, :session => "SESSION")
response = service.post("taobao.item.img.upload", :num_iid=>'1489161932', :image => File.open('example.jpg'))

Integrate with OmniAuth

require 'taobao/top-oauth' # includes TOP API and Oauth

and Rails initializer at config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :taobao, TAOBAO_API_KEY, TAOBAO_API_SECRET
end

and Append a line in routes.rb

get '/auth/:provider/callback', to: 'sessions#create'

and Add following lines in your sessions_controller.rb to initialize a user's session

def create
  @user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])
  session[:current_user] = @user
  redirect_to '/'
end

How to fetch a user's information from TOP API after 'token callback'

By default, the 'taobao_user_role' option is nil in OmniAuth::Strategies::Taobao class, in this case, your will get a simple raw-info hash like this:

{
  "user_id": TAOBAO_USER_ID,
  "nick": TAOBAO_USER_NICKNAME
}

and you can get a user's information through a TOP API method named 'taobao.user.get' like this:

OmniAuth::Strategies::Taobao.option :taobao_user_role, 'user'

and to use 'taobao.user.buyer.get' like this:

OmniAuth::Strategies::Taobao.option :taobao_user_role, 'buyer'

and to use 'taobao.user.seller.get' like this:

OmniAuth::Strategies::Taobao.option :taobao_user_role, 'seller'

NOTE: you should have avaliable TOP API permissions for above settings. visit Taobao User API for more details.

SSL verify in Taobao sandbox environment:

when you are using Oauth in the 'tbsandbox' env, you should skip verify SSL certification as following:

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE 

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request