没有从MVc4中的linkedin api获取个人资料图片和电子邮件地址

时间:2015-08-17 11:36:22

标签: c# asp.net asp.net-mvc linkedin-api hammock

以下是我的代码,请告知我为何无法获取emailidprofile pic

public class LinkedInController : Controller
{
    public ActionResult index()
    {
       return AuthenticateToLinkedIn();
    }

    static string token_secret = "";
    public ActionResult AuthenticateToLinkedIn()
    {
        var credentials = new OAuthCredentials
        {
            CallbackUrl = "http://localhost:7326/Linkedin/callback",
            ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
            Verifier = "123456",
            Type = OAuthType.RequestToken
        };

        var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials };
        var request = new RestRequest { Path = "requestToken" };
        request.AddParameter("scope", "r_emailaddress");

        RestResponse response = client.Request(request);
        string content = response.Conten

        var contents = HttpUtility.ParseQueryString(response.Content)
        var  token = response.Content.Split('&')[0].Split('=')[1];

        token_secret=contents["oauth_token_secret"];              

       Response.Redirect("https://api.linkedin.com/uas/oauth/authorize?oauth_token=" + token);
       return null;
    }

    string token = "";
    string verifier = "";
    public ActionResult Callback()
    {    
        token = Request["oauth_token"];
        verifier = Request["oauth_verifier"];
        var credentials = new OAuthCredentials
        {
            ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
            Token = token,
            TokenSecret = token_secret,
            Verifier = verifier,
            Type = OAuthType.AccessToken,
            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            Version = "1.0"    
        };

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

        var request = new RestRequest { Path = "accessToken" };               
        request.AddParameter("scope", "r_emailaddress");            

        RestResponse response = client.Request(request);
        string content = response.Content;     

        var contents = HttpUtility.ParseQueryString(response.Content);

        var accessToken =contents["oauth_token"];
        var accessTokenSecret=contents["oauth_token_secret"];                  

        var people = new LinkedInService(accessToken, accessTokenSecret).GetCurrentUser();               

        String companyName = people.FirstName;
        return Content(companyName);            
    }
}

我正在使用吊床,我正在获取first namelast name以及title,并且我已经在LinkedIn门户中启用了r_emailadress,但我们无法获得这些信息。

1 个答案:

答案 0 :(得分:0)

您使用的唯一范围是r_emailaddress。要获得图片,我认为您需要使用r_basicprofile。我无法在文档中看到名为“emailid'”的字段,只有电子邮件地址。

https://developer.linkedin.com/docs/fields/basic-profile