Wednesday, November 28, 2012

LinkedIn REST URL (Step 2 - Obtain Authorization)

Hello again,

I wrote about how to obtain a request token for LinkedIn yesterday.
For those of you who would like to read it can view it at http://dailyprogrammingtalk.blogspot.com/2012/11/linkedin-rest-url-step-1-obtain-request.html

That was just step 1 of OAuth authentication.
Step 2 is simpler. After making a POST request to obtain a request token, we will receive a response similar to below:
oauth_token=abcd&oauth_token_secret=abc123&oauth_callback_confirmed=true&xoauth_request_auth_url=https%3A%2F%2Fapi.linkedin.com%2Fuas%2Foauth%2Fauthorize&oauth_expires_in=599

Parse the querystring:
string token = Regex.Match(token, @"oauth_token=([^&]+)").Groups[1].Value,
string tokenSecret = Regex.Match(token, @"oauth_token_secret=([^&]+)").Groups[1].Value


After getting the token and token secret, append the request token in the querystring and redirect users to:
https://api.linkedin.com/uas/oauth/authenticate?oauth_token=abcd

At this point, users will choose to allow us to access their LinkedIn info or not. If yes, users will be redirected to our callback url (this callback url was set up when we obtained our consumer key on developer.linkedin.com) similar to below:

http://localhost:12345/linkedincallback.aspx?oauth_token=abcd&oauth_verifier=94262

On the callback page, parse and store the values for oauth_token, oauth_verifier, and oauth_token_secret because we need those for step 3 of OAuth authentication.

Let's move on to Step 3: http://dailyprogrammingtalk.blogspot.com/2012/12/linkedin-rest-url-step-3-obtain-access.html

Tuesday, November 27, 2012

LinkedIn REST URL (Step 1 - Obtain Request Token)

Hello,

I have had the pleasure to deal with the LinkedIn REST URLs these past few days.
There are some Ruby, Python, etc wrappers, but there is none for .NET.
There's not a lot of examples in .NET either.
So, I decided to write my own wrapper with the help of OAuthBase.cs.

Now, the basic authentication was already documented on https://developer.linkedin.com/documents/authentication.
There are 3 steps to be authenticated by LinkedIn:

  1. Obtain a request token
  2. Let users authorize our access to their info (http://dailyprogrammingtalk.blogspot.com/2012/11/linkedin-rest-api-step-2-obtain.html)
  3. Obtain an access token
I will focus on getting a request token on this blog post.

One thing that's not clear is having 'scope' in the querystring parameters.
I kept getting the signature_invalid 401 error.
It says on the documentation that we only need to make a POST request to https://api.linkedin.com/uas/oauth/requestToken?scope=r_basicprofile+r_emailaddress, which is misleading. I have to use a space instead of the plus sign.

So, I made a POST request to 
https://api.linkedin.com/uas/oauth/requestToken?scope=r_basicprofile r_emailaddress

I generated a signature using the url above, but without a token and token secret.

OAuthBase oauth = new OAuthBase();
string linkedinUrl = "https://api.linkedin.com/uas/oauth/requestToken?scope=r_basicprofile r_emailaddress"
string url = String.Empty;
string urlParameters = String.Empty;
string timeStamp = oauth.GenerateTimeStamp();
string nonce = oauth.GenerateNonce();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(linkedinUrl);
request.Method = "POST";
string signature = oauth.GenerateSignature(new Uri(linkedinUrl), consumerKey, consumerSecret, String.Empty, String.Empty, "POST", timeStamp, nonce, out url, out urlParameters);


Then, I created an 'Authorization' header for the request, which contains:

StringBuilder header = new StringBuilder("OAuth ");
header.AppendFormat("oauth_consumer_key=\"{0}\"", consumerKey);
header.AppendFormat(", oauth_signature_method=\"{0}\"""HMAC-SHA1");
header.AppendFormat(", oauth_signature=\"{0}\"", MsSecurity.Encoder.UrlEncode(signature));
header.AppendFormat(", oauth_nonce=\"{0}\"", nonce);
header.AppendFormat(", oauth_timestamp=\"{0}\"", timeStamp);
header.AppendFormat(", oauth_version=\"{0}\"""1.0");


The MsSecurity is an alias for the AntiXss library. I need to url-encode the signature. Otherwise, it won't work.

Now, I no longer get the 401 error. Hope this helps someone obtain their request token on LinkedIn.

Let's move on to Step 2: http://dailyprogrammingtalk.blogspot.com/2012/11/linkedin-rest-url-step-2-obtain.html