class RakutenProductApi::Authenticate
Constants
- REFRESH_TOKEN_LEEWAY
Attributes
access_expires_at[RW]
access_token[RW]
consumer_key[RW]
consumer_secret[RW]
password[RW]
sid[RW]
username[RW]
Public Class Methods
new(sid: RakutenProductApi.sid, username: RakutenProductApi.username, password: RakutenProductApi.password, consumer_key: RakutenProductApi.consumer_key, consumer_secret: RakutenProductApi.consumer_secret, access_token: nil, access_expires_at: nil)
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 12 def initialize(sid: RakutenProductApi.sid, username: RakutenProductApi.username, password: RakutenProductApi.password, consumer_key: RakutenProductApi.consumer_key, consumer_secret: RakutenProductApi.consumer_secret, access_token: nil, access_expires_at: nil) @sid = sid @username = username @password = password @consumer_key = consumer_key @consumer_secret = consumer_secret @access_token = access_token @access_expires_at = access_expires_at end
Public Instance Methods
api_request_auth()
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 38 def api_request_auth res = auth_request( "https://api.rakutenmarketing.com/token", { grant_type: "password", username: @username, password: @password, scope: @sid } ) process_auth_response(res) @access_expires_at = Time.now.to_i + @expires_in end
auth_header()
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 29 def auth_header ensure_authentication "Bearer #{@access_token}" end
auth_request(url, payload)
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 72 def auth_request(url, payload) uri = URI(url) Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| req = Net::HTTP::Post.new(uri) req["Authorization"] = "Basic #{request_auth_token}" req.set_form_data(payload) http.request(req) end end
ensure_authentication()
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 83 def ensure_authentication if @access_expires_at.nil? # puts "NIL: getting auth" api_request_auth elsif Time.now.to_i > @access_expires_at # puts "EXPIRED: getting auth" api_request_auth elsif Time.now.to_i > (@access_expires_at + REFRESH_TOKEN_LEEWAY) # puts "REFRESH LEEWAY: getting auth" refresh_api_request_auth end end
process_auth_response(res)
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 60 def process_auth_response(res) if res.code == "200" doc = JSON.parse(res.body) @expires_in = doc["expires_in"]&.to_i @refresh_token = doc["refresh_token"] @access_token = doc["access_token"] else puts "RESPONSE CODE #{res.code} received" res end end
refresh_api_request_auth()
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 49 def refresh_api_request_auth res = auth_request( "https://api.rakutenmarketing.com/token", { grant_type: "refresh_token", refresh_token: @refresh_token, scope: "Production" } ) process_auth_response(res) @access_expires_at = Time.now.to_i + @expires_in end
request_auth_token()
click to toggle source
# File lib/rakuten_product_api/authenticate.rb, line 34 def request_auth_token Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}").strip end