Using LinkedIn oAuth2 API in ASP.NET

Having spent the better part of Saturday figuring out how to create a new post in my LinkedIn group via ASP.NET, I thought I’d share what I did to save others the trouble.

Issue 1:  POST-ing a new group post, but how do I get the post ID?

Code Snippet
  1. string postId = string.Empty;
  2. string groupPostURL = string.Format(“https://api.linkedin.com/v1/groups/{0}/posts”, groupId);
  3. StringBuilder url = newStringBuilder();
  4. url.Append(groupPostURL);
  5. url.Append(“?oauth2_access_token=” + GetAccessToken() );
  6. JsonServiceClient.HttpWebRequestFilter = x =>
  7.             x.Headers.Add(“Content-Type”, “application/json”);
  8. JsonServiceClient.HttpWebRequestFilter = x =>
  9.             x.Headers.Add(“x-li-format”, “json”);
  10. var client = newJsonServiceClient(“https://api.linkedin.com”);
  11. string newUrl = string.Format(“/v1/groups/{0}/posts”, groupId) + “?oauth2_access_token=” + GetAccessToken();
  12. //string responseJson = client.Post<string>(newUrl, groupPostContent);
  13. HttpWebResponse webResponse = client.Post<HttpWebResponse>(url.ToString(), json);
  14. if (webResponse.StatusCode == HttpStatusCode.Created)
  15. {
  16.     string location = webResponse.Headers[“Location”];
  17.     if (location != null)
  18.     {
  19.         int lastIndex = location.LastIndexOf(‘/’);
  20.         if (lastIndex > 0)
  21.         {
  22.             lastIndex++;
  23.             postId = location.Substring(lastIndex, (location.Length – lastIndex));
  24.         }
  25.     }
  26. }
  27. return postId;

It turns out the newly created LinkedIn Post ID is returned in the header under the “Location” field. However, RestSharp does not give me access to the Location header (even when turning off FollowRedirects) so I used ServiceStack’s JSON client and returning an HttpWebResponse object. Once I have that, I parse the Location header field to get the Post ID.

Issue 2: How to post a job to my LinkedIn Group?

You need to do this in two steps. First step is explained above and once you have the Post ID you go onto “flag” this new post as a job.

Code Snippet
  1. string linkedInURL = string.Format(“https://api.linkedin.com/v1/posts/{0}/category/code?oauth2_access_token={1}”, postId, GetAccessToken());
  2. RestClient rc = newRestClient();
  3. RestRequest request = newRestRequest(linkedInURL, Method.PUT);
  4. request.AddHeader(“Content-Type”, “application/xml”);
  5. request.AddHeader(“x-li-format”, “xml”);
  6. request.RequestFormat = DataFormat.Xml;
  7. request.AddParameter(“text/xml”, “<code>job</code>”, ParameterType.RequestBody);
  8. RestResponse restResponse = (RestResponse)rc.Execute(request);
  9. ResponseStatus responseStatus = restResponse.ResponseStatus;
Posted in asp.net, coding
3 comments on “Using LinkedIn oAuth2 API in ASP.NET
  1. Sachin says:

    Hi,

    This is a very useful post.. can you please post the implementation of GetAccessToken()method. it is very important for me.

  2. Sachin says:

    Thanks Michael for reply.
    I knew that how to get the access token, Thought you have implemented it in .Net.

Leave a Reply

Your email address will not be published. Required fields are marked *

*