Spring.NET Social LinkedIn offers integration with LinkedIn's REST API through the ILinkedIn interface and its implementation, LinkedInTemplate.
Creating an instance of LinkedInTemplate involves invoking its constructor,
passing in the application's OAuth credentials, an access token/secret pair authorizing the application to act on a user's behalf
and the type of access level configured with the application.
For example:
string consumerKey = "..."; // The application's consumer key string consumerSecret = "..."; // The application's consumer secret string accessToken = "..."; // The access token granted after OAuth authorization string accessTokenSecret = "..."; // The access token secret granted after OAuth authorization ILinkedIn linkedIn = new LinkedInTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
You can also get an instance of ILinkedIn from the LinkedInServiceProvider class.
The example code below shows use of the LinkedInServiceProvider to retrieve an instance of ILinkedIn
after authenticating through OAuth1 server-side flow:
LinkedInServiceProvider serviceProvider = new LinkedInServiceProvider("consumerKey", "consumerSecret"); OAuth1Operations oauthOperations = serviceProvider.AuthOperations; OAuthToken requestToken = oauthOperations.FetchRequestToken("http://my-callback-url/", null); string authorizeUrl = oauthOperations.BuildAuthorizeUrl(requestToken, null); Response.Redirect(authorizeUrl); // upon receiving the callback from the provider: OAuthToken accessToken = oauthOperations.ExchangeForAccessToken(new AuthorizedRequestToken(requestToken, oauthVerifier), null); ILinkedIn linkedInApi = serviceProvider.GetApi(accessToken.Value, accessToken.Secret);
Once you have a ILinkedIn, you can perform a several operations against LinkedIn.
ILinkedIn is defined as follows:
public interface ILinkedIn : IApiBinding { ICommunicationOperations CommunicationOperations { get; } IConnectionOperations ConnectionOperations { get; } IProfileOperations ProfileOperations { get; } IRestOperations RestOperations { get; } }
The first three properties return sub-APIs, partitioning the LinkedIn service API into divisions targeting specific facets of LinkedIn functionality. These sub-APIs are defined by interfaces described in Table 2.1, “LinkedIn's Sub-APIs”.
| Sub-API Interface | Description |
|---|---|
| CommunicationOperations | Sending messages and connection requests. |
| ConnectionOperations | Retrieving connections. |
| ProfileOperations | Retrieving and performing operations on profiles. |
The last property RestOperations gets the underlying RestTemplate object
allowing for consumption of LinkedIn endpoints that may not be otherwise covered by the API binding.
Until the whole API is covered, extra converters have been added : XElementHttpMessageConverter,
DataContractHttpMessageConverter and DataContractJsonHttpMessageConverter.
An example is shown below using Company Search API:
string rawResult = linkedIn.RestOperations.GetForObject<string>("company-search?keywords=linkedin"); XElement xmlResult = linkedIn.RestOperations.GetForObject<XElement>("company-search?keywords=linkedin"); var result = from x in xmlResult.Descendants("companies").Elements() select x.Element("name");
There are 3 ways to call a method depending on the target Framework:
Available on .NET 2.0 and .NET 3.5
LinkedInProfile profile = linkedIn.ProfileOperations.GetUserProfile(); Console.WriteLine("Hi " + profile.DisplayName + "!");
Available on .NET 2.0, .NET 3.5 and Windows Phone
LinkedInProfile profile = linkedIn.ProfileOperations.GetUserProfileAsync(
r =>
{
Console.WriteLine("Hi " + r.Response.DisplayName + "!");
});
Available on .NET 4.0
LinkedInProfile profile = linkedIn.ProfileOperations.GetUserProfileAsync()
.ContinueWith(task =>
{
Console.WriteLine("Hi " + task.Result.DisplayName + "!");
});
For complete details on the Spring.NET Social's entire LinkedIn API binding,
refer to the API documentation from the "doc/api" directory of the distribution
and to the LinkedIn REST API documentation.
Samples are provided in the 'examples' directory of the distribution.