diff --git a/lib/rest_client.js b/lib/rest_client.js index 6db954c..cd9ca7d 100644 --- a/lib/rest_client.js +++ b/lib/rest_client.js @@ -19,6 +19,16 @@ module.exports.RestClient = function (options) { public: options.accessToken, secret: options.accessTokenSecret }; + function buildHeader(request_data, request_token = '') { + if (request_token) { + return { 'Authorization': 'Bearer ' + request_token }; + } + if (oauth.consumer.public && oauth.consumer.secret) { + return oauth.toHeader(oauth.authorize(request_data, token)) + } + + return {}; + } function apiCall(request_data, request_token = '') { logger.debug('Calling API endpoint: ' + request_data.method + ' ' + request_data.url + ' token: ' + request_token); @@ -26,7 +36,7 @@ module.exports.RestClient = function (options) { logger.info({ url: request_data.url, method: request_data.method, - headers: request_token ? { 'Authorization': 'Bearer ' + request_token } : oauth.toHeader(oauth.authorize(request_data, token)), + headers: buildHeader(request_token), json: true, body: request_data.body, }); @@ -35,7 +45,7 @@ module.exports.RestClient = function (options) { request({ url: request_data.url, method: request_data.method, - headers: request_token ? { 'Authorization': 'Bearer ' + request_token } : oauth.toHeader(oauth.authorize(request_data, token)), + headers: buildHeader(request_token), json: true, body: request_data.body, }, function (error, response, body) {