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
388 views
in Technique[技术] by (71.8m points)

c# - Retrieve Linkedin email address

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


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

...