简体   繁体   中英

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

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

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

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.

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.

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.

Use this api:

    https://api.linkedin.com/v1/people/~:(id,firstName,lastName,num-connections,picture-url,emailAddress)?format=json

    login() {
        let scopes:any = ['r_basicprofile', 'r_emailaddress', 'rw_company_admin', 'w_share'];
        this.linkedin.login(scopes, true)
        .then(() => {
            this.linkedin.getRequest('people/~:(id,firstName,lastName,num-connections,picture-url,emailAddress)')
            .then(res => {
                this.selfData = res;
            })
            .catch(e => {
                console.log(e);
            });
        })
        .catch(e => {
            console.log('Error logging in', e);
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM