Module: Mercadolibre::Core::Auth

Included in:
Api
Defined in:
lib/mercadolibre/core/auth.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(auth_code) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mercadolibre/core/auth.rb', line 14

def authenticate(auth_code)
  response = post_request('/oauth/token', {
    grant_type: 'authorization_code',
    client_id: @app_key,
    client_secret: @app_secret,
    code: auth_code,
    redirect_uri: @callback_url
  })[:body]

  @access_token = response['access_token']

  Mercadolibre::Entity::Auth.new({
    access_token: @access_token,
    refresh_token: response['refresh_token'],
    expired_at: response['expires_in'].to_i.seconds.from_now
  })
end

#authenticate_urlObject


4
5
6
7
8
9
10
11
12
# File 'lib/mercadolibre/core/auth.rb', line 4

def authenticate_url
  get_attrs = {
    response_type: 'code',
    client_id: @app_key,
    redirect_uri: CGI.escape(@callback_url)
  }.map { |k,v| "#{k}=#{v}" }.join('&')

  "#{@auth_url}/authorization?#{get_attrs}"
end

#update_token(refresh_token) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mercadolibre/core/auth.rb', line 32

def update_token(refresh_token)
  response = post_request('/oauth/token', {
    grant_type: 'refresh_token',
    client_id: @app_key,
    client_secret: @app_secret,
    refresh_token: refresh_token
  })[:body]

  @access_token = response['access_token']

  Mercadolibre::Entity::Auth.new({
    access_token: @access_token,
    refresh_token: response['refresh_token'],
    expired_at: response['expires_in'].to_i.seconds.from_now
  })
end