Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linkedin-api-v2): parse response bodies correctly #1

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions lib/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,60 @@ Strategy.prototype.userProfile = function(accessToken, done) {

try {
var json = JSON.parse(body);
var emails = json.elements;
var emails = [];
var elements = json["elements"];
elements.forEach(function (element) {
// elements can be both emails and phone numbers
// See: https://developer.linkedin.com/docs/guide/v2/people/primary-contact-api
// under Retrieve Email Address / Response Body
if (element && element["handle"].includes("emailAddress")) {
var email = {value: element["handle~"]["emailAddress"]};
emails.push(email);
}
});
self._fetchAsyncProfile(accessToken, done, emails);

} catch(e) {
done(e);
}
});
} else {
this._fetchAsyncProfile(accessToken, done);
}
} catch(e) {
done(e);
}
});
} else {
this._fetchAsyncProfile(accessToken, done);
}
};

Strategy.prototype._fetchAsyncProfile = function(accessToken, done, emails) {
this._oauth2.get(this.profileUrl, accessToken, function (err, body, res) {
this._oauth2.get(this.profileUrl, accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }

try {
var json = JSON.parse(body);

var profile = { provider: 'linkedin' };

// fields are now represented as MultiLocaleString object type
// See: https://docs.microsoft.com/en-us/linkedin/shared/references/v2/profile/lite-profile
var prefferedLanguage = json.firstName.preferredLocale.language;
var prefferedCountry = json.firstName.preferredLocale.country;
var prefferedLocaleKey = prefferedLanguage + '_' + prefferedCountry;


var lastName = json["lastName"]["localized"][prefferedLocaleKey];
var firstName = json["firstName"]["localized"][prefferedLocaleKey];

profile.id = json.id;
profile.displayName = json.formattedName;
// display name is not existent anymore
// See: https://docs.microsoft.com/en-us/linkedin/shared/references/v2/profile/lite-profile
profile.displayName = firstName + ' ' + lastName;

profile.name = {
familyName: json.lastName,
givenName: json.firstName
};
familyName: lastName,
givenName: firstName
};
profile.emails = emails || [{ value: json.emailAddress }];
profile.photos = [];
if (json.pictureUrl) {
profile.photos.push({ value: json.pictureUrl });
profile.photos.push({ value: json.pictureUrl });
}
profile._raw = body;
profile._json = json;
Expand Down Expand Up @@ -89,13 +112,12 @@ Strategy.prototype._convertScopeToUserProfileFields = function(scope, profileFie
var fields = [];

// To obtain pre-defined field mappings
if(Array.isArray(scope) && profileFields === null)
{
if(Array.isArray(scope) && profileFields === null) {
if(scope.indexOf('r_liteprofile') === -1){
scope.unshift('r_liteprofile');
}

if(scope.indexOf('r_emailaddress') !== -1){
if(scope.indexOf('r_emailaddress') !== -1) {
self.emailUrl = r_emailaddress
}

Expand All @@ -113,7 +135,7 @@ Strategy.prototype._convertScopeToUserProfileFields = function(scope, profileFie
}

return fields.join(',');
}
};



Expand All @@ -126,7 +148,7 @@ Strategy.prototype.authorizationParams = function(options) {
params['state'] = options.state;
}
return params;
}
};



Expand Down