上一章节大概总结整理了一下具体开发一个Linkedin应用的一些步骤。
这一章节主要是第一个步骤的获取Request token。 通过请求https://api.linkedin.com/uas/oauth/requestToken 获取request token。
然而,请求获取一个Request token必须以下要求及的带请求参数
HTTP Method (POST)---post方式提交申请
Request URI (https://api.linkedin.com/uas/oauth/requestToken)--请求地址
oauth_callback --- 验证成功后回调跳转的地址
oauth_consumer_key ---你申请的API KEY
oauth_nonce --随机数
oauth_signature_method --加密方式
oauth_timestamp --时间截签名
oauth_version --版本号
oauth_signature --签名
同时,这一步也分为三个小步骤:
1. 创建POST请求
2. 根据请求创建签名
3 .获取request token
好了,我们先看看第一步的POST请求,而这个个POST请求,需要类似下面的格式:
3Dhttp % 253A % 252F % 252Fwww. 163 .com % 26oauth_consumer_key
% 3D8R4adR12 - cVx7ySHO4tS62zkzYaSHVxhV0XY6lpyjCQn9YjR - nGn1OOmqdNdka_k
% 26oauth_nonce % 3D2063151 % 26oauth_signature_method % 3DHMAC - SHA1 % 26
oauth_timestamp % 3D1274640507 % 26oauth_version % 3D1. 0
当这个请求成功后,Linkedin就会输出带参数的字符串(如下), 那么oauth_token就是我们所需要的Request token了
& oauth_token_secret = 56db820b - 6ffc - 408f - 81f7 - 6aee9f39b8cd
& oauth_callback_confirmed = true
& xoauth_request_auth_url = https % 3A % 2F % 2Fapi.linkedin.com % 2Fuas % 2Foauth % 2Fauthorize
& oauth_expires_in = 599
那么,废话不多说,直接上代码,代码实在太长了,偶不想用太多文字去解释跟描述,代码就是最好的解释,开发人员最喜欢的就是代码了。
如果太多文字的话,估计文字加代码的方式长长地拖下去让人看得晕晕的。里面都比较简单,下面的代码中,我们就可以得到了Request token了。
string method, string callback, out string requestUrl, out string parameters)
{
string nonce = random.Next( 123400 , 9999999 ).ToString();
TimeSpan ts = DateTime.UtcNow - new DateTime( 1970 , 1 , 1 , 0 , 0 , 0 , 0 );
string timeStamp = Convert.ToInt64(ts.TotalSeconds).ToString();
requestUrl = string .Format( " {0}://{1}{2} " , url.Scheme,url.Host, url.AbsolutePath);
StringBuilder sb = new StringBuilder();
string requestparameters = @" oauth_callback={0}&oauth_consumer_key={1}&oauth_nonce={2}
&oauth_signature_method=HMAC-SHA1&oauth_timestamp={3}&oauth_version=1.0 " ;
sb.AppendFormat(requestparameters, UrlEncode(callback),consumerKey, nonce, timeStamp);
parameters = sb.ToString();
sb = new StringBuilder();
sb.AppendFormat( " {0}& " , method.ToUpper());
sb.AppendFormat( " {0}& " , UrlEncode(requestUrl));
sb.AppendFormat( " {0} " , UrlEncode(parameters));
return sb.ToString();
}
public void GenSign(Uri url, string _key, string _secret,
string token, string tokenSecret, string method, string callback,
out string requestUrl, out string parameters)
{
string sigstring = GenSignString(url, _key, token, tokenSecret,
method, callback, out requestUrl, out parameters);
HMACSHA1 ha = new HMACSHA1();
ha.Key = Encoding.ASCII.GetBytes( string .Format( " {0}&{1} " ,
UrlEncode(_secret), string .IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));
byte [] _buffer = System.Text.Encoding.ASCII.GetBytes(sigstring);
byte [] _bytes = ha.ComputeHash(_buffer);
string sig = Convert.ToBase64String(_bytes);
parameters = parameters + " &oauth_signature= " + HttpUtility.UrlEncode(sig);
}
public string GetRequestToKen()
{
string rquestUrl = string .Empty;
string query = string .Empty;
string callback = " http://localhost/linkedin/callback.aspx " ;
string url = " https://api.linkedin.com/uas/oauth/requestToken " ;
Uri uri = new Uri(url);
GenSign(uri, this .ConsumerKey, this .ConsumerSecret, string .Empty,
string .Empty, " POST " , callback, out rquestUrl, out query);
string response = WebRequest( " POST " , rquestUrl, query);
NameValueCollection qs = HttpUtility.ParseQueryString(response);
return qs[ " oauth_token " ];
}
public string WebRequest( string method, string url, string postData)
{
HttpWebRequest webRequest = null ;
StreamWriter requestWriter = null ;
string response = string .Empty;
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method;
webRequest.ServicePoint.Expect100Continue = false ;
webRequest.Timeout = 10000 ;
if (method.ToUpper() == " POST " )
{
webRequest.ContentType = " application/x-www-form-urlencoded " ;
requestWriter = new StreamWriter(webRequest.GetRequestStream());
try
{
requestWriter.Write(postData);
} catch { throw ;}
finally {
requestWriter.Close();
requestWriter = null ;
}}
response = WebResponseGet(webRequest);
webRequest = null ;
return response;
}
可是,问题来了,显然,代码之乱实在无法忍受,大部分硬编码编上去,虽然我们得到了结果(request token),
会为接下来的工作但变得更烦锁,为了得到更好的扩展,里面类似硬编码的参数跟变量(象POST之类)必须要改
(当然,个人在实际编码中确实改过很多东西,这个只是很早期的试验探索版本)