2

I'm using the Hammock library and C# to get the basic profile and email address of user from linked in following post here .

I've also followed these posts here and here

However for the life of me I cannot get the email address of the linkedin user.

Reading this linkedin post I got new created a new application to get new keys however still no luck.

Went through this linkedin post but could not get it to work either

I'm posting the code below but it's heavily lifted form the links I followed

var credentials = new OAuthCredentials
            {

                CallbackUrl = "http://localhost:2715/Callback.aspx",

                ConsumerKey = "my consumer key",//not shown

                ConsumerSecret = "my consumer secret",//not shown

                Type = OAuthType.RequestToken,

            };


        var client = new RestClient
            {
                Authority = "https://api.linkedin.com/uas/oauth",
                Credentials = credentials
            };

        //var scope = HttpUtility.UrlEncode("r_basicprofile r_emailaddress");

        var request = new RestRequest
                        {
                            Path = "requestToken?scope=r_basicprofile+r_emailaddress",
                        };

I get this screen when user navigates to linkedin.

enter image description here

To actually request the email I use this code below

 var request = new RestRequest { Path = "people/~/email-address" };

        var credentials = new Hammock.Authentication.OAuth.OAuthCredentials

        {

            Type = OAuthType.AccessToken,

            SignatureMethod = OAuthSignatureMethod.HmacSha1,

            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,

            ConsumerKey = "my consumer key", //not shown

            ConsumerSecret = "my consumer secret", //not shown

            Token = Session["AccessToken"].ToString(),

            TokenSecret = Session["AccessSecretToken"].ToString(),

            Verifier = Session["Verifier"].ToString()

        };


        var client = new RestClient()
        {
            Authority = "http://api.linkedin.com/v1/",
            Credentials = credentials,
            Method = WebMethod.Get
        };


        var MyInfo = client.Request(request);

        String content = MyInfo.Content;

This is the error I get on MyInfo.Content

enter image description here

Thanks in advance for your help.

On trying Kamyar's suggestion I get the following error message. What else should I try?

enter image description here

Hamza Ahmed
  • 1,571
  • 4
  • 18
  • 35

6 Answers6

2

Email address is accessible for the authenticated user as long as you request "r_emailaddress" permissions upon authorization.

Take a look at the Oauth Login Dialog that's presented to the user to authorize the application. Notice how "email address" isn't presented as one of the profile fields that the application wants to access. You'll need to modify this line in your code:

var request = new RestRequest
                        {
                            Path = "requestToken?scope=r_basicprofile+r_emailaddress",
                        };

To this:

var request = new RestRequest
                        {
                            Path = "requestToken?scope=r_basicprofile%20r_emailaddress",
                        };

The space should be URL encoded in your request token path.

Thanks, Kamyar Mohager

Kamyar Mohager
  • 709
  • 3
  • 10
1

Update: LinkedIn has updated their API and now you can get the email address in their normal API.

Deepak
  • 36
  • 4
  • I'm using their new [API](https://developer.linkedin.com/documents/authentication). @Deepak, is that the correct link or do they have a new(er) API that I'm not aware of as yet? – Hamza Ahmed Oct 17 '12 at 05:48
1

Using JavaScript, in the head area I have:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: put-your-api-key-here
    authorize: true
    scope: r_basicprofile r_emailaddress
</script>
<script type="text/javascript">

function loadData() {
    IN.API.Profile("me")
        .fields(["firstName","lastName","id","emailAddress"])
        .result(function(result) {
                //$("#profile").html(JSON.stringify(result))
                $("#profile").html(JSON.stringify(result))
        });
}

</script>

and in the body I put the following:

<div id="profile">
</div>
<script type="IN/Login" data-onAuth="loadData"></script>
BevSDC
  • 11
  • 1
1

I have found solution. Simply, do not include ?scope=r_emailaddres (or other permissions) to .Path property of RestRequest. Use something like this:

Dim request = New RestRequest With {.Path = "requestToken"}
request.AddParameter("scope", "r_emailaddress")

(vb net syntax)

.AddParameter method is the key.

Tomq
  • 1,085
  • 11
  • 14
0

I spent days looking into this and sadly found out that LinkedIn do not expose users email address through their API, unless you have a partner agreement with them (like Microsoft, etc.)

Knowing this your options are to use LinkedIn as an OpenId / OAuth provider and once authenticated ask them for their preferred email address.

Kane
  • 16,471
  • 11
  • 61
  • 86
  • If you look at this [post](https://developer.linkedin.com/forum/can-we-get-email-address) @Kane some users like Sorin Neacsu and Tomasz Cejner confirm that they have got it working with the new API. Additionally Quentin Auvray and Jens Uffhaus got it working if you look [here](https://developer.linkedin.com/forum/permission-scope-request-token-query-not-working) I'm unaware if these guys have partner agreement though. – Hamza Ahmed Oct 17 '12 at 05:58
  • The API changed some time late 2012: https://developer.linkedin.com/documents/authentication# – ptim Feb 12 '13 at 06:30
0

I finally got email address by using DotNetOpenAuth Api and a LinkedIn Consumer class I found at google dotnetopenauth group.

Hamza Ahmed
  • 1,571
  • 4
  • 18
  • 35