LinkedIn is a powerful tool to develop professional relationships. Using its tools you can also increase your marketing efficiency.
1. Installation
PM> Install-Package Sparkle.LinkedInNET
2. Create API client with configuration
// create from config file
Get a LinkedIn API key.
// create from config file
var config = LinkedInApiConfiguration.FromAppSettings("LogiDemo.LinkedInConnect");
// get the APIs client
var api = new LinkedInApi(config);
3. Create OAuth2 authorize url
protected void Page_Load(object sender, EventArgs e)
{
// create from config file
var config = LinkedInApiConfiguration.FromAppSettings("LogiDemo.LinkedInConnect");
// or manually
// var config = LinkedInApiConfiguration("api key", "api secret key");
// get the APIs client
var api = new LinkedInApi(config);
var scope = AuthorizationScope.ReadBasicProfile | AuthorizationScope.ReadEmailAddress;
var state = Guid.NewGuid().ToString();
var redirectUrl = "http://localhost:49965/WebForm1.aspx";
var url = api.OAuth2.GetAuthorizationUrl(scope, state, redirectUrl);
Response.Redirect(url.OriginalString);
}
4. Get the access token
if (Request.QueryString["code"] != null)
{
var accessCode = Request.QueryString["code"]; // save this code for each user in db and use this code for
// further accessing
var redirectUrl = "http://localhost:49965/WebForm1.aspx";
// this section of code is using to access the web
var config = LinkedInApiConfiguration.FromAppSettings("LogiDemo.LinkedInConnect");
var api = new LinkedInApi(config);
var userToken = api.OAuth2.GetAccessToken(accessCode, redirectUrl);
}
5. Example call: fetch user profile
var userToken = api.OAuth2.GetAccessToken(accessCode, redirectUrl);
var profile = api.Profiles.GetMyProfile(user, acceptLanguages, fields);
6. Field selectors
var user = new UserAuthorization(userToken.AccessToken);
string culture = "en-US";
var acceptLanguages = new string[] { culture ?? "en-US", "fr-FR", };
var fields = FieldSelector.For()
.WithId()
.WithFirstName()
.WithLastName()
.WithFormattedName()
.WithEmailAddress()
.WithHeadline()
.WithLocationName()
.WithLocationCountryCode()
.WithPictureUrl()
.WithPublicProfileUrl()
.WithSummary()
.WithIndustry()
.WithPositions()
.WithPositionsSummary()
.WithThreeCurrentPositions()
.WithThreePastPositions()
.WithProposalComments()
.WithAssociations()
.WithInterests()
.WithLanguageId()
.WithLanguageName()
.WithLanguageProficiency()
.WithCertifications()
.WithEducations()
.WithFullVolunteer()
.WithPatents()
.WithRecommendationsReceivedWithAdditionalRecommenderInfo()
.WithDateOfBirth()
.WithPhoneNumbers()
.WithImAccounts()
.WithPrimaryTwitterAccount()
.WithTwitterAccounts()
.WithSkills();
var profile = api.Profiles.GetMyProfile(user, acceptLanguages, fields);
var firstName = profile.Firstname;
var lastName = profile.Lastname;
var emailAddress = profile.EmailAddress;
var headine = profile.Headline;
var location = profile.Location.Name;
var industry = profile.Industry;
var dateOfDirth = profile.DateOfBirth;
}