API.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. const request = require('request');
  3. const { clientId, clientSecret, authorizationURL, redirectURI, accessTokenURL, scopePerms } = require('../config');
  4. class API {
  5. static getLinkedinId(info) {
  6. return new Promise((resolve, reject) => {
  7. const url = 'https://api.linkedin.com/v2/me';
  8. const headers = {
  9. 'Authorization': 'Bearer ' + info.token,
  10. 'cache-control': 'no-cache',
  11. 'X-Restli-Protocol-Version': '2.0.0'
  12. };
  13. request.get({ url: url, headers: headers }, (err, response, body) => {
  14. if(err) {
  15. reject(err);
  16. }
  17. resolve(JSON.parse(body));
  18. });
  19. });
  20. }
  21. static getCompanyFollowers(info) {
  22. return new Promise((resolve, reject) => {
  23. const param = encodeURIComponent(`urn:li:organization:`)+info.companyID+'?edgeType=CompanyFollowedByMember'
  24. const url = `https://api.linkedin.com/v2/networkSizes/${param}`;
  25. const headers = {
  26. 'Authorization': 'Bearer ' + info.token,
  27. 'cache-control': 'no-cache',
  28. 'X-Restli-Protocol-Version': '2.0.0'
  29. };
  30. request.get({ url: url, headers: headers }, (err, response, body) => {
  31. console.log(body);
  32. if(err || body.status !== 200) {
  33. console.log("besoin de reject");
  34. return reject(err);
  35. }
  36. return resolve(JSON.parse(body));
  37. });
  38. });
  39. }
  40. static getAccessToken(req) {
  41. const { code } = req.query;
  42. const body = {
  43. grant_type: 'authorization_code',
  44. code,
  45. redirect_uri: redirectURI,
  46. client_id: clientId,
  47. client_secret: clientSecret
  48. };
  49. return new Promise((resolve, reject) => {
  50. request.post({url: accessTokenURL, form: body }, (err, response, body) => {
  51. if(err) reject(err);
  52. resolve(JSON.parse(body));
  53. });
  54. });
  55. }
  56. static getAuthorizationUrl() {
  57. const state = Buffer.from(Math.round( Math.random() * Date.now() ).toString() ).toString('hex');
  58. const scope = encodeURIComponent(scopePerms.join(' '));
  59. const url = `${authorizationURL}?response_type=code&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectURI)}&state=${state}&scope=${scope}`;
  60. return url;
  61. }
  62. }
  63. module.exports = API;