Twitterizer-远程服务器返回错误:(401)未经授权

时间:2012-01-31 19:07:25

标签: c# asp.net-mvc-3 twitter twitter-oauth twitterizer

我正在关注Ricky的Twitterizer example(我的结尾有一些修改),但是当我尝试发送身份验证请求时,我收到了“401 Unauthorized”异常:

{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=}

关于可能导致此问题的任何想法?

1 个答案:

答案 0 :(得分:8)

登录dev.twitter.com并仔细检查您的应用程序是否为Web应用程序。为了成为一个Web应用程序,你必须在那里提供一个回调URL,即使你打算在运行时提供一个“真正的”回调。

仅供参考,您无法为注册的回调网址提供“localhost”,但您可以使用127.0.0.1。