1、獲取Request token。
2、用戶認證。
3、獲取Access token。
4、獲取用戶信息。
在處理OAuth授權過程中我也碰到幾個在新浪開放平臺論壇中常見的幾個問題,在這裏總結下,在後面講解中會講到我的想法和解決辦法:
1、requesttoken時callback問題。
2、401錯誤。
3、403錯誤。
4、500錯誤。
5、未經授權錯誤。
在這裏順便講壹下調用新浪微博接口必須是要申請壹個應用的,申請應用成功之後會得到壹個App key號和App Secret號,我們也需要通過這兩個參數來請求授權,還有就是網上有OAuthBase下載,但是要下對版本,我的Demo中也有,我們的授權主要的代碼是在OAuthBase.cs文件中的。
1、獲取Request token:
直接上代碼:
public void getRequestToken()
{
Uri uri = new Uri(requestTokenUri);
string nonce = oAuth.GenerateNonce();//獲取隨機生成的字符串,防止攻擊
string timeStamp = oAuth.GenerateTimeStamp();//發起請求的時間戳
string normalizeUrl, normalizedRequestParameters;
// 簽名
string sig = oAuth.GenerateSignature(uri, apiKey, apiKeySecret, string.Empty, string.Empty,
"GET", timeStamp, nonce, string.Empty, out normalizeUrl, out normalizedRequestParameters);
sig = HttpUtility.UrlEncode(sig);
//構造請求Request Token的url
StringBuilder sb = new StringBuilder(uri.ToString());
sb.AppendFormat("?oauth_consumer_key={0}&", apiKey);
sb.AppendFormat("oauth_nonce={0}&", nonce);
sb.AppendFormat("oauth_signature={0}&", sig);
sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
sb.AppendFormat("oauth_timestamp={0}&", timeStamp);
sb.AppendFormat("oauth_version={0}", "1.0");
//請求Request Token
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string responseBody = stream.ReadToEnd();
stream.Close();
response.Close();
int intOTS = responseBody.IndexOf("oauth_token=");
int intOTSS = responseBody.IndexOf("&oauth_token_secret=");
Session["oauth_token"] = responseBody.Substring(intOTS + 12, intOTSS - (intOTS + 12));
Session["oauth_token_secret"] = responseBody.Substring((intOTSS + 20), responseBody.Length - (intOTSS + 20));
Response.Redirect(AUTHORIZE + "?oauth_token=" + Session["oauth_token"] + "&oauth_callback=" + Request.Url);
}
我在請求Request token的時候遇到了401錯誤和地址返回錯誤,地址返回錯誤比較好解決,壹般都是地址錯誤,所以我直接用了Request.Url,那麽401錯誤了我出錯是在簽名 的地方,最開始的OAuthBase文件下載錯了,下給最新的就可以了,還有就是在請求參數中的oauth_version參數,有很多值是1.0a,這樣好像是不行的,全部改成1.0就能避免很多錯誤。
2、用戶認證:
在Request token請求成功之後,平臺自動跳到登錄頁面,進行用戶認證,認證通過之後平臺會將oauth_token和oauth_verifier返回到指定的callback來,將兩個參數保存下來用於請求Access token,在這裏如果地址不正確是會報錯的。
3、獲取Access token:
這個請求的重點還是在簽名,必須要將用戶認證後返回的oauth_token和oauth_verifier壹並簽名才能正確,有些OAuthBase中是沒有將verifier加入簽名當中當時讓我好生郁悶,如果這點錯了應該會報未經授權或者403錯誤,請求成功之後需要將oauth_token和oauth_token_secret重新保存下,下面是代碼:
public void getAccessToken(string requestToken, string oauth_verifier)
{
Uri uri = new Uri(ACCESS_TOKEN);
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizeUrl, normalizedRequestParameters;
// 簽名
string sig = oAuth.GenerateSignature(
uri,
apiKey,
apiKeySecret,
requestToken,
Session["oauth_token_secret"].ToString(),
"Get",
timeStamp,
nonce,
oauth_verifier,
out normalizeUrl,
out normalizedRequestParameters);
sig = oAuth.UrlEncode(sig);
//構造請求Access Token的url
StringBuilder sb = new StringBuilder(uri.ToString());
sb.AppendFormat("?oauth_consumer_key={0}&", apiKey);
sb.AppendFormat("oauth_nonce={0}&", nonce);
sb.AppendFormat("oauth_timestamp={0}&", timeStamp);
sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
sb.AppendFormat("oauth_version={0}&", "1.0");
sb.AppendFormat("oauth_signature={0}&", sig);
sb.AppendFormat("oauth_token={0}&", requestToken);
sb.AppendFormat("oauth_verifier={0}", oauth_verifier);
//請求Access Token
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string responseBody = stream.ReadToEnd();
stream.Close();
response.Close();
int intOTS = responseBody.IndexOf("oauth_token=");
int intOTSS = responseBody.IndexOf("&oauth_token_secret=");
int intUser = responseBody.IndexOf("&user_id=");
Session["oauth_token"] = responseBody.Substring(intOTS + 12, intOTSS - (intOTS + 12));
Session["oauth_token_secret"] = responseBody.Substring((intOTSS + 20), intUser - (intOTSS + 20));
Session["User_Id"] = responseBody.Substring((intUser + 9), responseBody.Length - (intUser + 9));
verify_credentials();
}
4、獲取登錄用戶信息:
步驟簡單和以上幾個請求方式也壹樣,主要是要將oauth_token和oauth_token_secret加入簽名,下面是代碼:
public void verify_credentials()
{
Uri uri = new Uri("url");
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizeUrl, normalizedRequestParameters;
// 簽名
string sig = oAuth.GenerateSignature(
uri,
apiKey,
apiKeySecret,
Session["oauth_token"].ToString(),
Session["oauth_token_secret"].ToString(),
"Get",
timeStamp,
nonce,
string.Empty,
out normalizeUrl,
out normalizedRequestParameters);
sig = HttpUtility.UrlEncode(sig);
StringBuilder sb = new StringBuilder(uri.ToString());
sb.AppendFormat("?oauth_consumer_key={0}&", apiKey);
sb.AppendFormat("oauth_nonce={0}&", nonce);
sb.AppendFormat("oauth_timestamp={0}&", timeStamp);
sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
sb.AppendFormat("oauth_version={0}&", "1.0");
sb.AppendFormat("oauth_signature={0}&", sig);
sb.AppendFormat("oauth_token={0}&", Session["oauth_token"].ToString());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string responseBody = stream.ReadToEnd();
stream.Close();
response.Close();
Session["responseBody"] = responseBody;
}
到這裏妳可以獲取用戶的個人信息,那麽OAuth授權也就成功,其實步驟是比較簡單的,主要要註意的就是簽名,簽名不正確壹定是通過不了的,還有就是壹些細節,如地址,版本號,請求方式這些細心點就能避免,由於時間原因這裏講的比較簡單,希望大家互相交流下,這裏是Demo:SinaOAuth
轉載僅供參考,版權屬於原作者。祝妳愉快,滿意請采納哦