From 0282929fa82f27250f01de1642e617484a8df9a6 Mon Sep 17 00:00:00 2001 From: efattal Date: Mon, 1 Jul 2024 15:40:09 +0200 Subject: [PATCH 1/2] Fix code --- linkedin-client.js | 2 +- linkedin-server.js | 2 +- package.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/linkedin-client.js b/linkedin-client.js index 206cb68..f0fe498 100644 --- a/linkedin-client.js +++ b/linkedin-client.js @@ -28,7 +28,7 @@ Linkedin.requestCredential = function(options, credentialRequestCompleteCallback scope = requestPermissions.join('+'); } else { // If extra permissions not passed, we need to request basic, available to all - scope = 'r_emailaddress+r_liteprofile'; + scope = 'email+profile'; } const loginStyle = OAuth._loginStyle('linkedin', config, options); diff --git a/linkedin-server.js b/linkedin-server.js index f619c98..e9f46db 100644 --- a/linkedin-server.js +++ b/linkedin-server.js @@ -90,7 +90,7 @@ const getTokenResponse = (query) => { // Request available fields from r_liteprofile const getIdentity = (accessToken) => { try { - const url = encodeURI(`https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))&oauth2_access_token=${accessToken}`); + const url = encodeURI(`https://api.linkedin.com/v2/userinfo?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))&oauth2_access_token=${accessToken}`); return HTTP.get(url).data; } catch (err) { throw new Error(`Failed to fetch identity from Linkedin. ${err.message}`); diff --git a/package.js b/package.js index c1be986..f9b3e7e 100644 --- a/package.js +++ b/package.js @@ -1,8 +1,8 @@ Package.describe({ - name: 'codifytools:linkedin-oauth', + name: 'efattal:linkedin-oauth', version: '1.0.4', summary: 'Linkedin OAuth flow', - git: 'https://github.com/codifytools/meteor-linkedin-oauth', + git: 'https://github.com/efattal/meteor-linkedin-oauth', documentation: null }); From e3bb762d7c88c64c5757b4a1bc881e57566779a7 Mon Sep 17 00:00:00 2001 From: efattal Date: Mon, 1 Jul 2024 18:32:40 +0200 Subject: [PATCH 2/2] Align api response --- linkedin-server.js | 56 +++++++++------------------------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/linkedin-server.js b/linkedin-server.js index e9f46db..e109412 100644 --- a/linkedin-server.js +++ b/linkedin-server.js @@ -1,35 +1,5 @@ Linkedin = {}; -const getImage = (profilePicture) => { - const image = []; - - if (profilePicture !== undefined){ - for (const element of profilePicture['displayImage~'].elements) { - for (const identifier of element.identifiers) { - image.push(identifier.identifier); - } - } - } - - return { - displayImage: profilePicture ? profilePicture.displayImage : null, - identifiersUrl: image - }; -} - -// Request for email, returns array -const getEmails = (accessToken) => { - const url = encodeURI(`https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))&oauth2_access_token=${accessToken}`); - const response = HTTP.get(url).data; - const emails = []; - - for (const element of response.elements) { - emails.push(element['handle~'].emailAddress); - } - - return emails; -}; - // checks whether a string parses as JSON const isJSON = (str) => { try { @@ -90,7 +60,7 @@ const getTokenResponse = (query) => { // Request available fields from r_liteprofile const getIdentity = (accessToken) => { try { - const url = encodeURI(`https://api.linkedin.com/v2/userinfo?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))&oauth2_access_token=${accessToken}`); + const url = encodeURI(`https://api.linkedin.com/v2/userinfo?oauth2_access_token=${accessToken}`); return HTTP.get(url).data; } catch (err) { throw new Error(`Failed to fetch identity from Linkedin. ${err.message}`); @@ -102,33 +72,27 @@ OAuth.registerService('linkedin', 2, null, query => { const { accessToken } = response; const identity = getIdentity(accessToken); - const { id, firstName, lastName, profilePicture } = identity; + const { sub, name, given_name, family_name, picture, email, email_verified } = identity; - if (!id) { + if (!sub) { throw new Error('Linkedin did not provide an id'); } const serviceData = { - id, + id: sub, accessToken, expiresAt: +new Date() + 1000 * response.expiresIn, }; - const emails = getEmails(accessToken); - const fields = { - linkedinId: id, - firstName, - lastName, - profilePicture: getImage(profilePicture), - emails + name, + firstName: given_name, + lastName: family_name, + picture, + email, + verified_email: email_verified }; - if (emails.length) { - const primaryEmail = emails[0]; - fields.emailAddress = primaryEmail; // for backward compatibility with previous versions of this package - fields.email = primaryEmail; - } Object.assign(serviceData, fields);