Skip to content

Instantly share code, notes, and snippets.

@mizrael
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizrael/74b4962ecf2a90d5cd36 to your computer and use it in GitHub Desktop.
Save mizrael/74b4962ecf2a90d5cd36 to your computer and use it in GitHub Desktop.
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