Reputation: 1811
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.
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
Thanks in advance for your help.
On trying Kamyar's suggestion I get the following error message. What else should I try?
Upvotes: 2
Views: 4732
Reputation: 1125
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.
Upvotes: 1
Reputation: 11
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>
Upvotes: 1
Reputation: 1811
I finally got email address by using DotNetOpenAuth Api and a LinkedIn Consumer class I found at google dotnetopenauth group.
Upvotes: 0
Reputation: 709
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
Upvotes: 2
Reputation: 36
Update: LinkedIn has updated their API and now you can get the email address in their normal API.
Upvotes: 1
Reputation: 16812
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.
Upvotes: 0