123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- '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,
- '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,
- 'cache-control': 'no-cache',
- 'X-Restli-Protocol-Version': '2.0.0'
- };
- request.get({ url: url, headers: headers }, (err, response, body) => {
- console.log(body);
- if(err || body.status !== 200) {
- console.log("besoin de reject");
- return reject(err);
- }
- return resolve(JSON.parse(body));
- });
- });
- }
- static getAccessToken(req) {
- const { code } = req.query;
- 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;
|