From 33dc83cc7181bea356e9e76a587f1ea890db3d15 Mon Sep 17 00:00:00 2001 From: "Craig R. Hughes" Date: Fri, 13 Nov 2015 13:13:28 -0800 Subject: [PATCH] Handle linkedin members whose profile settings block public profile access --- lib/oauth2.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/oauth2.js b/lib/oauth2.js index 0bf50077..b72fb8b4 100644 --- a/lib/oauth2.js +++ b/lib/oauth2.js @@ -25,8 +25,22 @@ Strategy.prototype.userProfile = function(accessToken, done) { //LinkedIn uses a custom name for the access_token parameter this._oauth2.setAccessTokenName("oauth2_access_token"); + var strategy = this; this._oauth2.get(this.profileUrl, accessToken, function (err, body, res) { - if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); } + if (err) { + if(!strategy.profileUrl.match('public-profile-url')) { + return done(new InternalOAuthError('failed to fetch user profile', err)); + } else { // Retry without requesting the field 'public-profile-url' which errors out if the user has disabled their public profile + strategy.profileUrl = strategy.profileUrl.replace('public-profile-url',''); + strategy.profileUrl = strategy.profileUrl.replace(',,',','); + strategy.profileUrl = strategy.profileUrl.replace('(,','('); + strategy.profileUrl = strategy.profileUrl.replace(',)',')'); + + return strategy.userProfile(accessToken, function(err, profile) { + return done(err, profile); + }); + } + } try { var json = JSON.parse(body);