Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

javascript - How to extract a parsed email address from a LinkedIn API response?

I'm trying to setup a custom Auth0 LinkedIn Connection.

This is what I have for the Fetch User Profile Script:

function fetchUserProfile(accessToken, context, callback) {

  //Create the profile object
  const profile = {
    user_id: '',
    given_name: '',
    family_name: '',
    email: '',
  };

  // Call the Linkedin Primary Contact API
  request.get(
    {
      url: 'https://api.linkedin.com/v2/me',
      headers: {
        'Authorization': 'Bearer ' + accessToken,
      }
    },
    (err, resp, body) => {
      if (err) {
        return callback(err);
      }

      if (resp.statusCode !== 200) {
        return callback(new Error(body));
      }

      let bodyParsed;
      try {
        bodyParsed = JSON.parse(body);
      } catch (jsonError) {
        return callback(new Error(body));
      }

      profile.user_id = bodyParsed.id;
      profile.given_name = bodyParsed.localizedFirstName;
      profile.family_name = bodyParsed.localizedLastName;

    }
  );
  
  // Call the Linkedin Profile API
  request.get(
    {
      url: 'https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(primary,type,handle~))',
      headers: {
        'Authorization': 'Bearer ' + accessToken,
      }
    },
    (err, resp, body) => {
      if (err) {
        return callback(err);
      }

      if (resp.statusCode !== 200) {
        return callback(new Error(body));
      }

      let bodyParsed;
      try {
        bodyParsed = JSON.parse(body);
      } catch (jsonError) {
        return callback(new Error(body));
      }
    console.log(body);
    profile.email = bodyParsed.elements.handle~.emailAddress;

    }
  );

  //Send the profile object to the callback
  callback(null, profile);

}

It works (almost) but I'm having trouble putting emailAddress into the profile object.

The console.log(body); returns something like this (email redacted):

{
   "elements":[
      {
         "handle":"urn:li:emailAddress:102319820",
         "type":"EMAIL",
         "handle~":{
            "emailAddress":"[email protected]"
         },
         "primary":true
      }
   ]
}

The problem is that profile.email = bodyParsed.elements.handle~.emailAddress; gives an error because it uses the ~ symbol.

So how do I access emailAddress and insert it into the profile object?

question from:https://stackoverflow.com/questions/65837919/how-to-extract-a-parsed-email-address-from-a-linkedin-api-response

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...