Twitterizer-远程服务器返回错误:(401)未经授权 [英] Twitterizer- The remote server returned an error: (401) Unauthorized
问题描述
我下面Ricky的<一个href=\"http://stackoverflow.com/questions/8003959/mvc3-c-sharp-beginner-in-twitterizer-how-to-logon-user-via-twitter-and-get-some/8039457#8039457\">Twitterizer例如(与我结束了一些修改),但我得到一个401未授权的例外,当我尝试发送认证请求:
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);
}
请注意,我已经删除了一些code,它不属于错误的。以上code在异常孤单结果。有一点要注意的是,我的 RETURNURL
是空的(虽然同样的事情发生,如果我提供了一个 RETURNURL
太);一切似乎罚款。在的HttpWebRequest
是这样的:
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?
推荐答案
登录dev.twitter.com,并仔细检查你的应用程序是一个Web应用程序。为了成为一个Web应用程序,您必须提供一个回调的网址在那里,即使你要在运行时提供了一个真实的回调。
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.
仅供参考,不能用于注册回调URL提供的'localhost',但你可以使用127.0.0.1。
FYI, you cannot supply 'localhost' for the registered callback url, but you can use 127.0.0.1.
这篇关于Twitterizer-远程服务器返回错误:(401)未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!