12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 'use strict';
- const request = require('request');
- const { clientId, clientSecret, authorizationURL, redirectURI, accessTokenURL, scopePerms } = require('../config');
- class API {
- static getLinkedinId(info) {
- return new Promise((resolve, reject) => {
- const url = 'https://api.linkedin.com/v2/me';
- const headers = {
- 'Authorization': 'Bearer ' + info.token.access_token,
- 'cache-control': 'no-cache',
- 'X-Restli-Protocol-Version': '2.0.0'
- };
- request.get({ url: url, headers: headers }, (err, response, body) => {
- if(err) {
- reject(err);
- }
- resolve(JSON.parse(body));
- });
- });
- }
- static getCompanyFollowers(info) {
- return new Promise((resolve, reject) => {
- const param = encodeURIComponent(`urn:li:organization:`)+info.companyID+'?edgeType=CompanyFollowedByMember'
- const url = `https://api.linkedin.com/v2/networkSizes/${param}`;
- const headers = {
- 'Authorization': 'Bearer ' + info.token.access_token,
- 'cache-control': 'no-cache',
- 'X-Restli-Protocol-Version': '2.0.0'
- };
- request.get({ url: url, headers: headers }, (err, response, body) => {
- let parsedBody = JSON.parse(body)
- if(err || !parsedBody.firstDegreeSize) reject(err ? err : parsedBody.message);
- resolve(JSON.parse(body));
- });
- });
- }
- static getAccessToken(code) {
- const body = {
- grant_type: 'authorization_code',
- code,
- redirect_uri: redirectURI,
- client_id: clientId,
- client_secret: clientSecret
- };
- return new Promise((resolve, reject) => {
- request.post({url: accessTokenURL, form: body }, (err, response, body) => {
- if(err) reject(err);
- resolve(JSON.parse(body));
- });
- });
- }
- static getAuthorizationUrl() {
- const state = Buffer.from(Math.round( Math.random() * Date.now() ).toString() ).toString('hex');
- const scope = encodeURIComponent(scopePerms.join(' '));
- const url = `${authorizationURL}?response_type=code&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectURI)}&state=${state}&scope=${scope}`;
- return url;
- }
- }
- module.exports = API;
|