坛子里有莫有会OauthBase的啊,返回401错误帮忙看下

hyj12345678 2013-12-12 08:50:08
这段代码是要到server上做一个2条腿的Oauth认证,但是老是返回401错误,用的是OauthBase基类,在线等。
private string DoRequest1212()
{
string id = "UB7XDF"; //Request.Form["Id"].ToString();
string code = "U2D2N1G621"; // Request.Form["Code"].ToString();
string ip = Request.ServerVariables["REMOTE_ADDR"].ToString(); //"10.0.235.76"; //

var verifyUrl = "https://tcnc.eu/51RH/api/v2/verify/" + id + "-" + code;
var paras = "?user_ip=" + ip + "&user_agent=Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11";
string finalurl = verifyUrl + paras;

var authBase = new OAuth.OAuthBase();
var oauth_consumer_key = "VCOvW2eSbiq3kw6og4Jw";
var oauth_consumer_secret = "4Ln4/Flpe98oDVMhVwTVsyx5b2A+6KL";

string normalizedUrl = "";
string normalizedRequestParameters = "";
var oauth_nonce = authBase.GenerateNonce();
var oauth_timestamp = authBase.GenerateTimeStamp();
string sig = authBase.GenerateSignature(new Uri(finalurl), oauth_consumer_key, oauth_consumer_secret,
null, null, "GET", oauth_timestamp,
oauth_nonce, out normalizedUrl, out normalizedRequestParameters);

var webRequest = WebRequest.Create(finalurl) as HttpWebRequest;

var authorizationParas = new StringBuilder();
authorizationParas.Append("Authorization: OAuth ");
authorizationParas.Append("realm=\"tcnc.eu\"");
authorizationParas.AppendFormat(",oauth_consumer_key={0},", oauth_consumer_key);
authorizationParas.AppendFormat("oauth_nonce={0},", oauth_nonce);
authorizationParas.AppendFormat("oauth_timestamp={0},", oauth_timestamp);
authorizationParas.AppendFormat("oauth_signature_method={0},", "HMAC-SHA1");
authorizationParas.AppendFormat("oauth_version={0},", "1.0");
authorizationParas.AppendFormat("oauth_signature={0}", authBase.UrlEncode(sig));

webRequest.Headers.Add(authorizationParas.ToString());

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

return response.GetResponseStream().ToString();
}
...全文
149 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
易IT网 2013-12-13
  • 打赏
  • 举报
回复
之前做过类似这种OAuth接口,基本需要注意的就是请求的参数、参数的排序、大小写、签名方法等这些问题,如果都完全符合基本就行的通了。可通过输出authorizationParas.ToString()字符串,给第三方平台配合进行查找原因!QQ上有回复你,这里也留下,便提醒后来者。

5,655

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧