diff --git a/lib/hubspot-ruby.rb b/lib/hubspot-ruby.rb index 8df67e38..adcdad42 100644 --- a/lib/hubspot-ruby.rb +++ b/lib/hubspot-ruby.rb @@ -18,6 +18,7 @@ require 'hubspot/deal_properties' require 'hubspot/owner' require 'hubspot/engagement' +require 'hubspot/oauth' module Hubspot def self.configure(config={}) diff --git a/lib/hubspot/config.rb b/lib/hubspot/config.rb index 9d82f5ee..44256770 100644 --- a/lib/hubspot/config.rb +++ b/lib/hubspot/config.rb @@ -1,9 +1,13 @@ require 'logger' +require 'hubspot/connection' module Hubspot class Config - CONFIG_KEYS = [:hapikey, :base_url, :portal_id, :logger] + CONFIG_KEYS = [ + :hapikey, :base_url, :portal_id, :logger, :access_token, :client_id, + :client_secret, :redirect_uri + ] DEFAULT_LOGGER = Logger.new('/dev/null') class << self @@ -14,7 +18,18 @@ def configure(config) @hapikey = config["hapikey"] @base_url = config["base_url"] || "https://api.hubapi.com" @portal_id = config["portal_id"] - @logger = config['logger'] || DEFAULT_LOGGER + @logger = config["logger"] || DEFAULT_LOGGER + @access_token = config["access_token"] + @client_id = config["client_id"] if config["client_id"].present? + @client_secret = config["client_secret"] if config["client_secret"].present? + @redirect_uri = config["redirect_uri"] if config["redirect_uri"].present? + + unless access_token.present? ^ hapikey.present? + Hubspot::ConfigurationError.new("You must provide either an access_token or an hapikey") + end + if access_token.present? + Hubspot::Connection.headers("Authorization" => "Bearer #{access_token}") + end self end @@ -23,6 +38,8 @@ def reset! @base_url = "https://api.hubapi.com" @portal_id = nil @logger = DEFAULT_LOGGER + @access_token = nil + Hubspot::Connection.headers({}) end def ensure!(*params) diff --git a/lib/hubspot/connection.rb b/lib/hubspot/connection.rb index 7c87e2dc..82da162f 100644 --- a/lib/hubspot/connection.rb +++ b/lib/hubspot/connection.rb @@ -45,7 +45,11 @@ def log_request_and_response(uri, response, body=nil) end def generate_url(path, params={}, options={}) - Hubspot::Config.ensure! :hapikey + if Hubspot::Config.access_token.present? + options[:hapikey] = false + else + Hubspot::Config.ensure! :hapikey + end path = path.clone params = params.clone base_url = options[:base_url] || Hubspot::Config.base_url diff --git a/lib/hubspot/oauth.rb b/lib/hubspot/oauth.rb new file mode 100644 index 00000000..a582b8c4 --- /dev/null +++ b/lib/hubspot/oauth.rb @@ -0,0 +1,46 @@ +require 'httparty' +module Hubspot + class OAuth < Connection + include HTTParty + DEFAULT_OAUTH_HEADERS = {"Content-Type" => "application/x-www-form-urlencoded;charset=utf-8"} + class << self + def refresh(params) + params.stringify_keys! + no_parse = params.delete("no_parse") { false } + body = { + client_id: params["client_id"] || Hubspot::Config.client_id, + grant_type: "refresh_token", + client_secret: params["client_secret"] || Hubspot::Config.client_secret, + redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri, + refresh_token: params["refresh_token"] + } + response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS) + log_request_and_response oauth_url, response, body + raise(Hubspot::RequestError.new(response)) unless response.success? + + no_parse ? response : response.parsed_response + end + + def create(params) + params.stringify_keys! + no_parse = params.delete("no_parse") { false } + body = { + client_id: params["client_id"] || Hubspot::Config.client_id, + grant_type: "authorization_code", + client_secret: params["client_secret"] || Hubspot::Config.client_secret, + redirect_uri: params["redirect_uri"] || Hubspot::Config.redirect_uri, + code: params["code"] + } + response = post(oauth_url, body: body, headers: DEFAULT_OAUTH_HEADERS) + log_request_and_response oauth_url, response, body + raise(Hubspot::RequestError.new(response)) unless response.success? + + no_parse ? response : response.parsed_response + end + + def oauth_url + oauth_url = Hubspot::Config.base_url + "/oauth/v1/token" + end + end + end +end