【发布时间】:2011-10-22 04:27:30
【问题描述】:
我正在尝试让 OAuth 与 Google Data API 的 .NET 库一起使用。不幸的是,每当我调用 GetUnauthorizedRequestToken 时,都会收到 400 Bad Response 错误。这是我的代码...
OAuthParameters parameters = new OAuthParameters() {
ConsumerKey = DOMAIN_NAME,
ConsumerSecret = SECRET_KEY,
Scope = "https://docs.google.com/feeds/",
Callback = Request.RawUrl,
SignatureMethod = "HMAC-SHA1"
};
OAuthUtil.GetUnauthorizedRequestToken(parameters);
据我所知,我正确地遵循了这里的说明: http://code.google.com/apis/gdata/docs/auth/oauth.html
任何有关此问题的帮助将不胜感激!
编辑:太平洋标准时间 2011 年 9 月 10 日晚上 11:56
首先,非常感谢 cmets!
所以我已经摆弄了一下,让未经授权的请求令牌工作,但 OAuth 仍然没有真正工作......这是一个更完整的代码转储:-\
string token = Request["oauth_token"];
if (!String.IsNullOrEmpty(token)) {
OAuthParameters tParams = new OAuthParameters() {
ConsumerKey = DOMAIN_NAME,
ConsumerSecret = SECRET_KEY,
Scope = S_SCOPE,
Callback = S_CALLBACK,
SignatureMethod = "HMAC-SHA1"
};
tParams.Verifier = Request["oauth_verifier"];
tParams.Token = token;
try {
// http://code.google.com/apis/gdata/docs/auth/oauth.html
// 1. Extract token from the callback URL
//OAuthUtil.UpdateOAuthParametersFromCallback(Request.Url.Query, parameters);
// 2. Upgrade to an access token
OAuthUtil.GetAccessToken(tParams);
string accessToken = tParams.Token;
string accessTokenSecret = tParams.TokenSecret;
Session["sp"] = tParams; // don't worry, we don't even get here yet
return RedirectToAction("List");
}
catch (System.Net.WebException ex) {
// print out tons of stuff (removed for sanity)
}
//... and start over again
}
try {
OAuthParameters parameters = new OAuthParameters() {
ConsumerKey = DOMAIN_NAME,
ConsumerSecret = SECRET_KEY,
Scope = S_SCOPE,
Callback = S_CALLBACK,
SignatureMethod = "HMAC-SHA1"
};
OAuthUtil.GetUnauthorizedRequestToken(parameters);
string approvalPageUrl = OAuthUtil.CreateUserAuthorizationUrl(parameters);
ViewBag.AuthUrl = approvalPageUrl;
}
catch (System.Net.WebException ex) {
// print out more stuff
}
这是我看到的错误(稍作修改以删除敏感数据,但我保留所有符号原样以防有人认为这是编码错误):
X-Content-Type-Options = nosniff
X-XSS-Protection = 1; mode=block
Content-Length = 386
Cache-Control = private, max-age=0
Content-Type = text/plain; charset=UTF-8
Date = Sun, 11 Sep 2011 06:53:26 GMT
Expires = Sun, 11 Sep 2011 06:53:26 GMT
Server = GSE
/accounts/OAuthGetAccessToken
signature_invalid
base_string:GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetAccessToken&oauth_consumer_key%3Dmydomain.com%26oauth_nonce%3D4432dc4bd59b4ea0b133ea52cb450062%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1315724016%26oauth_token%3D4%252FGeEWOLvQL_eGlj8zAdrzi7YPhmhS%26oauth_verifier%3DMkGYPy8qeZPRg7gLKKXsYIiM%26oauth_version%3D1.0
Callback = http://mydomain.com/auth
ConsumerKey = mydomain.com
ConsumerSecret = RxGavGhuXi92sy3F-Q3DKcY_
Nonce = 4430dc4bd59b4ea3b133ea52cb450062
Scope = https://docs.google.com/feeds
SignatureMethod = HMAC-SHA1
Timestamp = 1315724016
Token = 4/GeAWOLvQL_eGlj1zEerzi7YPhmhS
TokenSecret =
Verifier = MkXLPy8qeZARq7aLKXKsYIiM
【问题讨论】:
-
顺便说一句,我尝试过使用 HTTP 和 HTTPS 协议
-
您的消费者密钥和回调 url 在同一个域中?
-
您是否在应用程序设置中启用了您的域?