How do I get email address field using the LinkedIn Javascript API?
Asked Answered
R

4

9

I'm using the LinkedIn Javascript API to sign in users to my application, however the API is not returning the email address even though I'm requiring permission for that specific field. I'm including the API script as follows:

<script type="text/javascript" src="//platform.linkedin.com/in.js">
  api_key: API_KEY
  scope: r_fullprofile r_emailaddress
</script>

then I'm including the Log In button in the markup:

<script type="in/Login" data-onAuth="onLinkedInAuth">

and finally I have a function to add the callback for the API response:

function onLinkedInAuth() {
    var fields = ['first-name', 'last-name', 'email-address'];

    IN.API.Profile("me").fields(fields).result(function(data) {
        console.log(data);
    }).error(function(data) {
        console.log(data);
    });
};

I'm only getting the First and Last Name but the API doesn't return the email field.

Reference: https://developer.linkedin.com/documents/profile-fields#email

Runner answered 7/11, 2013 at 18:23 Comment(0)
N
14

1- be sure you made email permission (r_emailaddress) in your app http://developer.linkedin.com/documents/authentication#granting

2- then you may use this

    <script type="text/javascript" src="http://platform.linkedin.com/in.js">
        api_key: key
        **onLoad: onLinkedInLoad**
        authorize: true
    </script>

    <script>



        function onLinkedInLoad() {
            IN.Event.on(IN, "auth", onLinkedInAuth);
        }

        // 2. Runs when the viewer has authenticated
        function onLinkedInAuth() {

            IN.API.Profile("me").fields("first-name", "last-name", "email-address").result(function (data) {
                console.log(data);
            }).error(function (data) {
                console.log(data);
            });
         }
</script>

hope this will help you :) thanks

Nihhi answered 25/12, 2013 at 10:35 Comment(0)
V
2

Hello there @Ulises Figueroa, May be I am coming in a bit late but this is how I had got this done:

Start off with the initial script tag on the top of your page within the head section:

<script>
    Client Id Number here:
    onLoad: onLinkedInLoad
    authorize: true
</script>

Then, in your JS File,(I had placed an external JS File to process this API sign up/ Auth), have the following details placed:

function onLinkedInLoad() {
    IN.Event.on(IN, "auth", getProfileData);
}

function onSuccess(data) {
    console.log(data);
}

function onError(error) {
    console.log(error);
}

    function getProfileData(){
        IN.API.Profile("me").fields(["firstName","lastName", "email-address", "positions"]).result(function(data) {
            var profileData = data.values[0];
            var profileFName = profileData.firstName;
            var profileLName = profileData.lastName;

            if(data.values[0].positions._total == "0" || data.values[0].positions._total == 0 || data.values[0].positions._total == undefined) {
                console.log("Error on position details");
                var profileCName = "Details Are Undefined";
            }
            else {
                var profileCName = profileData.positions.values["0"].company.name;
            }
            var profileEName = profileData.emailAddress;

            //console.log all the variables which have the data that 
            //has been captured through the sign up auth process and
            //you should get them...

        });
    }

Then last but not the least, add the following in your HTML DOCUMENT which can help you initiate the window popup for the linkedin auth sign up form:

<script type="in/Login"></script>

The above setup had worked for me. Sure this will help you out.

Cheers and have a nice day.

Vinnievinnitsa answered 6/2, 2017 at 5:14 Comment(1)
how did you do authentication with JS? Can you please guide me. I followed the same steps, I&#39;m able to login and get the user details. But I need to get access token and send back to my server api, so that from server we will check that LinkedIn accounts company updates.Ingenuity
E
1

Implementation looks good. I'd believe this is a result from the profile's privacy settings. Per linked-in's docs:

Not all fields are available for all profiles. The fields available depend on the relationship between the user you are making a request on behalf of and the member, the information that member has chosen to provide, and their privacy settings. You should not assume that anything other than id is returned for a given member.

Epimenides answered 8/11, 2013 at 0:18 Comment(0)
R
-1

I figured out that this only happens with certain LinkedIn accounts, so this might be caused because some privacy setting with the email. I couldn't find any reference to the documentation so I had to consider the case when email field is not available.

Runner answered 23/12, 2013 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.