Module: Mercadopago::Core::Oauth

Included in:
Api
Defined in:
lib/mercadopago/core/oauth.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
# File 'lib/mercadopago/core/oauth.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
  })

  @access_token = response.body.access_token
  response.body.expiration_time = (Time.now + response.body.expires_in.to_i)
  response.body
end

#authenticate_urlObject



4
5
6
7
8
9
10
11
12
# File 'lib/mercadopago/core/oauth.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

#create_tokenObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mercadopago/core/oauth.rb', line 41

def create_token
  headers = {
    content_type: 'application/x-www-form-urlencoded',
    accept: :json
  }

  response = post_request('/oauth/token', {
    grant_type: 'client_credentials',
    client_id: @app_key,
    client_secret: @app_secret
  }, headers)

  @access_token = response.body.access_token
  response.body.expiration_time = (Time.now + response.body.expires_in.to_i)
  response.body
end

#update_token(refresh_token) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mercadopago/core/oauth.rb', line 28

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
  })

  @access_token = response.body.access_token
  response.body.expiration_time = (Time.now + response.body.expires_in.to_i)
  response.body
end