2013-03-25 13 views
7

में DotNetOpenAuth का उपयोग करके लिंक्डइन पूर्ण प्रोफ़ाइल विवरण LinkedIn खाते का उपयोग करके मेरा एमवीसी 4 एप्लिकेशन लॉगिन की अनुमति देता है। मैं लॉग इन उपयोगकर्ता के लिंकइन से उपलब्ध सभी विवरण खींचना चाहता हूं। वर्तमान में मैंने निम्नलिखित किया है।एमवीसी 4

मेरे AuthConfig.cs में,

Dictionary<string, object> linkedInExtraData = new Dictionary<string, object>();   
     linkedInExtraData.Add("Icon", "../Images/linkedIn.png");   
     OAuthWebSecurity.RegisterClient(
      client: new App_Start.LinkedInCustomClient("xxxxxxxxxxxx", "yyyyyyyyyyyyyyy"), 
      displayName: "LinkedIn", 
      extraData: linkedInExtraData); 

linkedInCustomClient.cs में, लिंक्डइन डेवलपर किट से

public class LinkedInCustomClient : OAuthClient 
{ 
    private static XDocument LoadXDocumentFromStream(Stream stream) 
    { 
     var settings = new XmlReaderSettings 
     { 
      MaxCharactersInDocument = 65536L 
     }; 
     return XDocument.Load(XmlReader.Create(stream, settings)); 
    } 


    /// Describes the OAuth service provider endpoints for LinkedIn. 
    private static readonly ServiceProviderDescription LinkedInServiceDescription = 
      new ServiceProviderDescription 
      { 
       AccessTokenEndpoint = 
         new MessageReceivingEndpoint("https://api.linkedin.com/uas/oauth/accessToken", 
         HttpDeliveryMethods.PostRequest), 
       RequestTokenEndpoint = 
         new MessageReceivingEndpoint("https://api.linkedin.com/uas/oauth/requestToken?scope=r_fullprofile", 
         HttpDeliveryMethods.PostRequest), 
       UserAuthorizationEndpoint = 
         new MessageReceivingEndpoint("https://www.linkedin.com/uas/oauth/authorize", 
         HttpDeliveryMethods.PostRequest), 
       TamperProtectionElements = 
         new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }, 
       ProtocolVersion = ProtocolVersion.V10a 
      }; 

    public LinkedInCustomClient(string consumerKey, string consumerSecret) : 
     base("linkedIn", LinkedInServiceDescription, consumerKey, consumerSecret) { } 

    /// Check if authentication succeeded after user is redirected back from the service provider. 
    /// The response token returned from service provider authentication result. 
    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", 
     Justification = "We don't care if the request fails.")] 
    protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response) 
    { 
     // See here for Field Selectors API http://developer.linkedin.com/docs/DOC-1014 
     const string profileRequestUrl = 
      "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,interests,headline,industry,summary,email-address,location:(name),picture-url,positions,associations,languages,honors,educations,date-of-birth,primary-twitter-account,three-current-positions,three-past-positions,group-memberships,specialties,skills)"; 


     string accessToken = response.AccessToken; 
     string tokenSecret = (response as ITokenSecretContainingMessage).TokenSecret; 
     string Verifier = response.ExtraData.Values.First(); 


     var profileEndpoint = 
      new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest); 
     HttpWebRequest request = 
      WebWorker.PrepareAuthorizedRequest(profileEndpoint, accessToken); 

     try 
     { 
      using (WebResponse profileResponse = request.GetResponse()) 
      { 
       using (Stream responseStream = profileResponse.GetResponseStream()) 
       { 
        XDocument document = LoadXDocumentFromStream(responseStream); 

        return new AuthenticationResult(
         isSuccessful: true, 
         provider: ProviderName, 
         providerUserId: userId, 
         userName: userName, 
         extraData: extraData); 
       } 
      } 
     } 
     catch (Exception exception) 
     { 
      return new AuthenticationResult(exception); 
     } 
    } 

} 

मेरी नियंत्रक में,

AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); 
     if (!result.IsSuccessful) 
     { 
      return RedirectToAction("ExternalLoginFailure"); 
     } 

मैं निम्नलिखित प्राप्त करने की आवश्यकता प्रमाणीकरण परिणाम के रूप में मेरे नियंत्रक में विवरण।

(id,first-name,last-name,interests,headline,industry,summary,email-address,location:(name),picture-url,positions,associations,languages,honors,educations,date-of-birth,primary-twitter-account,three-current-positions,three-past-positions,group-memberships,specialties,skills) 
+0

क्या आपके पास इसका एक कार्यान्वयन कार्यान्वयन है? मैंने इंटरनेट को खराब कर दिया है और मेरे आवेदन में लिंक्डइन से डेटा खींचने के बारे में कोई अच्छा उदाहरण नहीं मिल रहा है। – ledgeJumper

उत्तर

5

लिंक्डइन से आपके अनुरोध की प्रतिक्रिया एक XML फ़ाइल होगी। प्रारूप और खेतों LinkedIn Profile Fields

में उल्लेख कर रहे ईमेल क्षेत्र में हो रही के लिए, आप के रूप में

RequestTokenEndpoint = new MessageReceivingEndpoint("https://api.linkedin.com/uas/oauth/requestToken?scope=r_fullprofile+r_emailaddress", HttpDeliveryMethods.PostRequest),

अपने अनुरोध टोकन URL संशोधित करने की आवश्यकता

आप के रूप में निम्नलिखित कोड

XDocument document = LoadXDocumentFromStream(responseStream); 
में आवश्यक फ़ील्ड प्राप्त कर सकते हैं

उदाहरण: xml फ़ाइल से पहला नाम फ़ील्ड प्राप्त करने के लिए,

var firstName = document.Root.Element("first-name").Value; 

भाषा, पदों, कौशल इत्यादि जैसे फ़ील्ड प्रोफ़ाइल के हिस्से के रूप में संरचित वस्तुओं के रूप में वापस आ जाएंगे।

उदाहरण: भाषा क्षेत्र।

var Lang = document.Root.Element("languages");       
    var languages = new List<string>(); 
    if (Lang != null) 
    { 
    foreach (var l in Lang.Elements()) 
     { 
      if (l.Element("language") != null && l.Element("language").Element("name") != null) 
      { 
      languages.Add(l.Element("language").Element("name").Value); 
      } 
     } 
     } 

फिर आप "अतिरिक्त डेटा" में फ़ील्ड जोड़ सकते हैं जिसे नियंत्रक में एक्सेस किया जा सकता है।

extraData.Add("firstName", firstName); 
extraData.Add("languages", lang);