From 04821e23fbfb063933153f41e17c8c1a5a4d71b1 Mon Sep 17 00:00:00 2001 From: "dmitriy.sushko" Date: Wed, 7 Dec 2016 16:30:27 +0200 Subject: [PATCH 1/2] Update the library to support the OAuth 2.0 authorization, by sending the "Authorization" header. --- lib/hubspot/hubspot.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/hubspot/hubspot.js b/lib/hubspot/hubspot.js index 8d3bfd5..3221479 100644 --- a/lib/hubspot/hubspot.js +++ b/lib/hubspot/hubspot.js @@ -62,7 +62,7 @@ hubspotAPI.prototype.execute = function (verbParams, path, availableParams, give var body; var currentParam; var parsedResponse; - var requestOptions; + var verb = verbParams && verbParams.verb || verbParams; var headers = { @@ -75,6 +75,12 @@ hubspotAPI.prototype.execute = function (verbParams, path, availableParams, give var finalParams = {}; + var requestOptions = { + method: verb, + headers: headers, + encoding: null + }; + for (var i = 0; i < availableParams.length; i++) { currentParam = availableParams[i]; if (typeof givenParams[currentParam] !== 'undefined') @@ -92,13 +98,10 @@ hubspotAPI.prototype.execute = function (verbParams, path, availableParams, give if (this.access_token) { authParams.access_token = this.access_token; + requestOptions.headers["Authorization"] = "Bearer " + this.access_token; } - requestOptions = { - method: verb, - headers: headers, - encoding: null - }; + // some endpoints use array-like parameters that must be stringified. // also must user querystring instead of qs. From bf23fab6041dec5ed0d9afb0089df173cc436a38 Mon Sep 17 00:00:00 2001 From: "dmitriy.sushko" Date: Thu, 8 Dec 2016 16:14:44 +0200 Subject: [PATCH 2/2] Reworked the old code to support current Authorization scheme, by removing the access_token from query string. --- lib/hubspot/hubspot.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/hubspot/hubspot.js b/lib/hubspot/hubspot.js index 3221479..75e9535 100644 --- a/lib/hubspot/hubspot.js +++ b/lib/hubspot/hubspot.js @@ -63,7 +63,6 @@ hubspotAPI.prototype.execute = function (verbParams, path, availableParams, give var currentParam; var parsedResponse; - var verb = verbParams && verbParams.verb || verbParams; var headers = { "Content-Type": this.contentType, @@ -88,16 +87,15 @@ hubspotAPI.prototype.execute = function (verbParams, path, availableParams, give } /** - * For OAuth: &access_token=TOKEN + * For OAuth 2.0: Set header "Authorization" = "Bearer " + TOKEN * For API Keys: &hapikey=KEY - * @see http://developers.hubspot.com/auth/oauth_overview + * @see http://developers.hubspot.com/docs/methods/auth/oauth-overview */ if (this.api_key) { - authParams.hapikey = this.api_key; + finalParams.hapikey = this.api_key; } if (this.access_token) { - authParams.access_token = this.access_token; requestOptions.headers["Authorization"] = "Bearer " + this.access_token; } @@ -122,9 +120,6 @@ hubspotAPI.prototype.execute = function (verbParams, path, availableParams, give requestOptions[verbParams.json ? 'json' : 'form'] = finalParams; uri = uri + "?" + qs.stringify(authParams); - } else { - var allParams = _.merge(authParams, finalParams); - } requestOptions.uri = uri;