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:
There are 3 steps to be authenticated by LinkedIn:
- Obtain a request token
- Let users authorize our access to their info (http://dailyprogrammingtalk.blogspot.com/2012/11/linkedin-rest-api-step-2-obtain.html)
- 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.
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
No comments:
Post a Comment