Class: Hubspot::OAuth

Inherits:
Connection show all
Includes:
HTTParty
Defined in:
lib/hubspot/oauth.rb

Constant Summary collapse

DEFAULT_OAUTH_HEADERS =
{"Content-Type" => "application/x-www-form-urlencoded;charset=utf-8"}

Class Method Summary collapse

Methods inherited from Connection

delete_json, get_json, post_json, put_json

Class Method Details

.authorize_url(scopes, params = {}) ⇒ Object


20
21
22
23
24
25
26
# File 'lib/hubspot/oauth.rb', line 20

def authorize_url(scopes, params={})
  client_id = params[:client_id] || Hubspot::Config.client_id
  redirect_uri = params[:redirect_uri] || Hubspot::Config.redirect_uri
  scopes = Array.wrap(scopes)

  "https://app.hubspot.com/oauth/authorize?client_id=#{client_id}&scope=#{scopes.join("%20")}&redirect_uri=#{redirect_uri}"
end

.create(code, params = {}, options = {}) ⇒ Object


15
16
17
18
# File 'lib/hubspot/oauth.rb', line 15

def create(code, params={}, options={})
  oauth_post(token_url, { grant_type: "authorization_code", code: code }.merge(params),
    options)
end

.oauth_post(url, params, options = {}) ⇒ Object


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

def oauth_post(url, params, options={})
  no_parse = options[:no_parse] || false

  body = {
    client_id: Hubspot::Config.client_id,
    client_secret: Hubspot::Config.client_secret,
    redirect_uri: Hubspot::Config.redirect_uri,
  }.merge(params)

  response = post(url, body: body, headers: DEFAULT_OAUTH_HEADERS)
  log_request_and_response url, response, body

  raise(Hubspot::RequestError.new(response)) unless response.success?

  no_parse ? response : response.parsed_response
end

.refresh(token, params = {}, options = {}) ⇒ Object


10
11
12
13
# File 'lib/hubspot/oauth.rb', line 10

def refresh(token, params={}, options={})
  oauth_post(token_url, { grant_type: "refresh_token", refresh_token: token }.merge(params),
    options)
end

.token_urlObject


28
29
30
# File 'lib/hubspot/oauth.rb', line 28

def token_url
  token_url = Hubspot::Config.base_url + "/oauth/v1/token"
end