Twitterizer- The remote server returned an error: (401) Unauthorized

懵懂的女人 提交于 2019-12-07 18:23:37

问题


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?


回答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.



来源:https://stackoverflow.com/questions/9085370/twitterizer-the-remote-server-returned-an-error-401-unauthorized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!