node-linkedin-v2
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -18,7 +18,9 @@ 'use strict'; | ||
* @param redirect_uri | ||
* @param apiHost | ||
* @param apiResource | ||
* @param api_host | ||
* @param api_resource | ||
* @param oauth_url | ||
*/ | ||
constructor(client_id, client_secret, redirect_uri, apiHost = 'https://api.linkedin.com', apiResource = '/v2') { | ||
this.url = apiHost + apiResource; | ||
constructor(client_id, client_secret, redirect_uri, api_host = 'https://api.linkedin.com', api_resource = '/v2', oauth_url = 'https://www.linkedin.com/oauth/v2') { | ||
this.url = api_host + api_resource; | ||
this.oauth_url = oauth_url; | ||
this.client_id = client_id; | ||
@@ -41,7 +43,21 @@ this.client_secret = client_secret; | ||
getAuthorizationUrl(scope, state) { | ||
if (!Array.isArray(scope)) throw new Error('Scope must be an array'); | ||
const scope_string = encodeURIComponent(scope.join(',')); | ||
return `https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&state=${state}&scope=${scope_string}`; | ||
return `${this.oauth_url}/authorization?response_type=code&client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&state=${state}&scope=${scope_string}`; | ||
} | ||
/** | ||
* Get access token: https://developer.linkedin.com/docs/oauth2 | ||
* | ||
* @param code | ||
* @param state | ||
* @returns {Promise<Object>} | ||
*/ | ||
async getAccessToken(code, state) { | ||
if (!code) throw new Error('Code parameter cannot be empty'); | ||
const url = `${this.oauth_url}/accessToken?grant_type=authorization_code&code=${code}&redirect_uri=${this.redirect_uri}&client_id=${this.client_id}&client_secret=${this.client_secret}`; | ||
return this.invoke('POST', url, { 'content-type': 'application/x-www-form-urlencoded' }); | ||
} | ||
/** | ||
* Find Total Number of Connections | ||
@@ -51,5 +67,6 @@ * | ||
*/ | ||
async getTotalConnectionsNumber() { | ||
async getTotalConnectionsNumber(access_token) { | ||
if (!access_token) throw new Error('Access code cannot be empty'); | ||
const url = `${this.url}/connections?q=viewer&start=0&count=0`; | ||
return this.invoke('GET', url) | ||
return this.invoke('GET', url, undefined, undefined, { withAuth: true, access_token }); | ||
} | ||
@@ -62,5 +79,6 @@ | ||
*/ | ||
async getCurrentMemberProfile() { | ||
async getCurrentMemberProfile(access_token) { | ||
if (!access_token) throw new Error('Access code cannot be empty'); | ||
const url = `${this.url}/me`; | ||
return this.invoke('GET', url) | ||
return this.invoke('GET', url, undefined, undefined, { withAuth: true, access_token }); | ||
} | ||
@@ -75,8 +93,14 @@ | ||
* @param {object} body - The JSON data to POST if applicable, or null | ||
* @param {object} auth - An object to pass to make a call which requires authorization, example { withAuth: true, access_token: 'access_token' } | ||
* | ||
* @returns {object} The body of the HTTP response | ||
*/ | ||
invoke(method, url, headers = { 'content-type': 'application/json' }, body = {}) { | ||
const options = this.generateOptions(method, url, headers, body); | ||
invoke(method, url, headers = { 'Content-Type': 'Application/json' }, body = {}, auth = { withAuth: false, access_token: null }) { | ||
return new Promise((resolve, reject) => { | ||
let options; | ||
try { | ||
options = this.generateOptions(method, url, headers, body, auth); | ||
} catch (err) { | ||
return reject(err); | ||
} | ||
request(options, (error, response, body) => { | ||
@@ -98,11 +122,12 @@ if(error) return reject(error); | ||
* @param {object} body - The JSON data to POST if applicable, or null | ||
* @param {object} auth - An object to pass to make a call which required authorization, example { withAuth: true, access_token: 'access_token' } | ||
* | ||
* @returns {object} The HttpOptions JSON object | ||
*/ | ||
generateOptions(method, url, headers, body) { | ||
const options = { | ||
url, | ||
method, | ||
headers, | ||
}; | ||
generateOptions(method, url, headers, body, auth) { | ||
if (auth.withAuth) { | ||
if (!auth.access_token) throw new Error('Missing required "access_token" in the auth body'); | ||
headers['Authorization'] = `Bearer ${auth.access_token}`; | ||
} | ||
const options = { url, method, headers }; | ||
if (body) options['body'] = JSON.stringify(body); | ||
@@ -109,0 +134,0 @@ return options; |
{ | ||
"name": "node-linkedin-v2", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Client for LinkedIn API v2", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10294
8
186