Twitterizer - 远程服务器返回错误:(401) 未经授权
我正在关注 Ricky 的 Twitterizer 示例(我这边做了一些修改),但是当我尝试发送身份验证请求:
{Twitterizer.TwitterizerException: The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at Twitterizer.WebRequestBuilder.ExecuteRequest()
at Twitterizer.OAuthUtility.GetRequestToken(String consumerKey, String consumerSecret, String callbackAddress)
--- End of inner exception stack trace ---
at Twitterizer.OAuthUtility.GetRequestToken(String consumerKey, String consumerSecret, String callbackAddress)
at MyProject.Controllers.AccountController.Authenticate(String oauthToken, String oauthVerifier, String returnUrl) in C:\path\to\my\website\Controllers\AccountController.cs:line 78}
我有保存我的消费者密钥和消费者秘密的私有成员变量:
// I obtain my consumer key and the consumer secret from the web config
private static readonly string _twitterConsumerKey = WebConfigurationManager.AppSettings["TwitterConsumerKey"];
private static readonly string _twitterConsumerSecret = WebConfigurationManager.AppSettings["TwitterConsumerSecret"];
而且我还有一个处理登录的控制器:
// GET: Account/Logon/
[HttpPost]
public ActionResult LogOn(string returnUrl)
{
return Authenticate(string.Empty, string.Empty, HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]));
}
//private ActionResult
private ActionResult Authenticate(string oauthToken, string oauthVerifier, string returnUrl)
{
if (string.IsNullOrEmpty(oauthToken) || string.IsNullOrEmpty(oauthVerifier))
{
UriBuilder builder = new UriBuilder(this.Request.Url);
builder.Query = string.Concat(
builder.Query,
string.IsNullOrEmpty(builder.Query) ? string.Empty : "&",
"returnUrl=",
returnUrl);
try
{
string token = OAuthUtility.GetRequestToken(
_twitterConsumerKey,
_twitterConsumerSecret,
builder.ToString()).Token; // <-- Throws the exception
return Redirect(OAuthUtility.BuildAuthorizationUri(token, true).ToString());
}
catch (Exception e)
{
ViewBag.Error = e.ToString(); // 401 Unauthorized
return View();
}
}
OAuthTokenResponse tokens = OAuthUtility.GetAccessToken(
_twitterConsumerSecret,
_twitterConsumerSecret,
oauthToken,
oauthVerifier);
// Save the user details and set the authentication (cookie)
// ...
// ...
// ...
if (string.IsNullOrEmpty(returnUrl))
return Redirect("/");
else
return Redirect(returnUrl);
}
请注意,我已经删除了一些与错误无关的代码。仅上面的代码就会导致异常。需要注意的一件事是我的 returnUrl
是空的(尽管如果我也提供 returnUrl
也会发生同样的情况);其他一切似乎都很好。 HttpWebRequest
如下所示:
Address = {https://api.twitter.com/oauth/request_token?oauth_callback=http://localhost:24465/Account/LogOn?returnUrl=}
关于可能导致此问题的原因有什么想法吗?
I'm following Ricky's Twitterizer example (with some modifications on my end), but I'm getting a "401 Unauthorized" exception when I try to send the authentication request:
{Twitterizer.TwitterizerException: The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at Twitterizer.WebRequestBuilder.ExecuteRequest()
at Twitterizer.OAuthUtility.GetRequestToken(String consumerKey, String consumerSecret, String callbackAddress)
--- End of inner exception stack trace ---
at Twitterizer.OAuthUtility.GetRequestToken(String consumerKey, String consumerSecret, String callbackAddress)
at MyProject.Controllers.AccountController.Authenticate(String oauthToken, String oauthVerifier, String returnUrl) in C:\path\to\my\website\Controllers\AccountController.cs:line 78}
I have private member variables which hold my consumer key and consumer secret:
// I obtain my consumer key and the consumer secret from the web config
private static readonly string _twitterConsumerKey = WebConfigurationManager.AppSettings["TwitterConsumerKey"];
private static readonly string _twitterConsumerSecret = WebConfigurationManager.AppSettings["TwitterConsumerSecret"];
And I also have a controller that handles the login:
// GET: Account/Logon/
[HttpPost]
public ActionResult LogOn(string returnUrl)
{
return Authenticate(string.Empty, string.Empty, HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]));
}
//private ActionResult
private ActionResult Authenticate(string oauthToken, string oauthVerifier, string returnUrl)
{
if (string.IsNullOrEmpty(oauthToken) || string.IsNullOrEmpty(oauthVerifier))
{
UriBuilder builder = new UriBuilder(this.Request.Url);
builder.Query = string.Concat(
builder.Query,
string.IsNullOrEmpty(builder.Query) ? string.Empty : "&",
"returnUrl=",
returnUrl);
try
{
string token = OAuthUtility.GetRequestToken(
_twitterConsumerKey,
_twitterConsumerSecret,
builder.ToString()).Token; // <-- Throws the exception
return Redirect(OAuthUtility.BuildAuthorizationUri(token, true).ToString());
}
catch (Exception e)
{
ViewBag.Error = e.ToString(); // 401 Unauthorized
return View();
}
}
OAuthTokenResponse tokens = OAuthUtility.GetAccessToken(
_twitterConsumerSecret,
_twitterConsumerSecret,
oauthToken,
oauthVerifier);
// Save the user details and set the authentication (cookie)
// ...
// ...
// ...
if (string.IsNullOrEmpty(returnUrl))
return Redirect("/");
else
return Redirect(returnUrl);
}
Note that I've removed some of the code that doesn't pertain to the error. The above code alone results in the exception. One thing to note is that my returnUrl
is empty (although the same thing happens if I provide a returnUrl
too); everything else seems fine. The HttpWebRequest
looks like this:
Address = {https://api.twitter.com/oauth/request_token?oauth_callback=http://localhost:24465/Account/LogOn?returnUrl=}
Any ideas on what may be causing this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
登录 dev.twitter.com 并仔细检查您的应用程序是否是 Web 应用程序。为了成为一个 Web 应用程序,您必须在那里提供一个回调 url,即使您将在运行时提供“真正的”回调。
仅供参考,您无法为注册的回调 URL 提供“localhost”,但可以使用 127.0.0.1。
Login to dev.twitter.com and double check that your application is a web application. In order to be a web application, you MUST supply a callback url there, even though you're going to provide a 'real' callback at runtime.
FYI, you cannot supply 'localhost' for the registered callback url, but you can use 127.0.0.1.