Class: EcwidApi::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/ecwid_api/o_auth.rb

Overview

Public: Authentication objects manage OAuth authentication with an Ecwid

store.

Examples

app = EcwidApi::Authentication.new do |config|
  # see initialize for configuration
end

app.oauth_url  # send the user here to authorize the app

token = app.access_token(params[:code]) # this is the code they provide
                                        # to the redirect_uri
token.access_token
token.store_id      # these are what you need to access the API

Constant Summary collapse

CONFIG =
%w(client_id client_secret scope redirect_uri)

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ OAuth

Public: Initializes a new Ecwid Authentication for OAuth

Examples

app = EcwidApi::Authentication.new do |config|
  config.client_id     = "some client id"
  config.client_secret = "some client secret"
  config.scope         "this_is_what_i_want_to_do oh_and_that_too"
  config.redirect_uri  = "https://example.com/oauth"
end

Yields:

  • (_self)

Yield Parameters:


36
37
38
39
40
41
# File 'lib/ecwid_api/o_auth.rb', line 36

def initialize
  yield(self) if block_given?
  CONFIG.each do |method|
    raise Error.new("#{method} is required to initialize a new EcwidApi::Authentication") unless send(method)
  end
end

Instance Method Details

#access_token(code) ⇒ Object

Public: Obtain the access token in order to use the API

code - the temporary code obtained from the authorization callback

Examples

token = app.access_token(params[:code])
token.access_token  # the access token that authenticates each API request
token.store_id      # the authenticated Ecwid store_id

Returns an OpenStruct which responds with the information needed to access the API for a store.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ecwid_api/o_auth.rb', line 64

def access_token(code)
  response = connection.post("/api/oauth/token",
    client_id:     client_id,
    client_secret: client_secret,
    code:          code,
    redirect_uri:  redirect_uri,
    grant_type:    "authorization_code"
  )

  if response.success?
    OpenStruct.new(response.body)
  else
    raise Error.new(response.body["error_description"])
  end
end

#oauth_urlObject

Public: The URL for OAuth authorization.

This is the URL that the user will need to go to to authorize the app with the Ecwid store.


48
49
50
# File 'lib/ecwid_api/o_auth.rb', line 48

def oauth_url
  "https://my.ecwid.com/api/oauth/authorize?" + oauth_query
end