Last active
August 29, 2015 14:13
-
-
Save mizrael/74b4962ecf2a90d5cd36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedInController : Controller | |
{ | |
public RedirectResult Profile() | |
{ | |
var redirectUrl = "http://mydomain/linkedin/profilereturn/"; | |
var url = GetAuthorizationUrl(redirectUrl); | |
return Redirect(url.ToString()); | |
} | |
public ActionResult ProfileReturn(string code, string state) | |
{ | |
var redirectUrl = "http://mydomain/linkedin/profilereturn/"; | |
var profile = ReadMyProfile(code, redirectUrl); | |
var jsonProfile = Newtonsoft.Json.JsonConvert.SerializeObject(profile); | |
return Content(jsonProfile); | |
} | |
private static Uri GetAuthorizationUrl(string redirectUrl) | |
{ | |
var api = CreateAPI(); | |
var scope = Sparkle.LinkedInNET.OAuth2.AuthorizationScope.ReadBasicProfile | | |
Sparkle.LinkedInNET.OAuth2.AuthorizationScope.ReadEmailAddress | | |
Sparkle.LinkedInNET.OAuth2.AuthorizationScope.ReadContactInfo; | |
var state = Guid.NewGuid().ToString(); | |
var url = api.OAuth2.GetAuthorizationUrl(scope, state, redirectUrl); | |
return url; | |
} | |
private static Person ReadMyProfile(string code, string redirectUrl) | |
{ | |
var api = CreateAPI(); | |
var userToken = api.OAuth2.GetAccessToken(code, redirectUrl); | |
var user = new Sparkle.LinkedInNET.UserAuthorization(userToken.AccessToken); | |
var fieldSelector = FieldSelector.For<Person>().WithFirstName() | |
.WithLastName() | |
.WithEmailAddress(); | |
var profile = api.Profiles.GetMyProfile(user, null, fieldSelector); | |
return profile; | |
} | |
private static LinkedInApi CreateAPI() | |
{ | |
var config = new LinkedInApiConfiguration(ConfigurationManager.AppSettings["LinkedIn_AppID"], | |
ConfigurationManager.AppSettings["LinkedIn_AppSecret"]); | |
var api = new LinkedInApi(config); | |
return api; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment