DotNetOpenAuth Twitter Authenticaion返回401 Unauthorized

时间:2013-04-13 12:20:53

标签: c# oauth twitter-oauth dotnetopenauth

我开始用Twitter撕掉我的头发并尝试登录用户!我有Facebook,谷歌,OpenId一切正常,只是Twitter是一个痛苦。

当我尝试运行我的代码时,我经常得到401 Unauthorized,因为我的生活无法找出原因。

我创建了一个推特客户端,并且我正在使用DotNetOpenAuth示例解决方案中的InMemoryTokenManager。我的Twitter客户端在这里

public class TwitterClient
{
    private string UserName { get; set; }

    private static readonly ServiceProviderDescription ServiceDescription =
        new ServiceProviderDescription
        {
            RequestTokenEndpoint = new MessageReceivingEndpoint(
                                       "https://api.twitter.com/oauth/request_token",
                                       HttpDeliveryMethods.GetRequest |
                                       HttpDeliveryMethods.AuthorizationHeaderRequest),

            UserAuthorizationEndpoint = new MessageReceivingEndpoint(
                                      "https://api.twitter.com/oauth/authorize",
                                      HttpDeliveryMethods.GetRequest |
                                      HttpDeliveryMethods.AuthorizationHeaderRequest),

            AccessTokenEndpoint = new MessageReceivingEndpoint(
                                      "https://api.twitter.com/oauth/access_token",
                                      HttpDeliveryMethods.GetRequest |
                                      HttpDeliveryMethods.AuthorizationHeaderRequest),

            TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
        };

    IConsumerTokenManager _tokenManager;

    public TwitterClient(IConsumerTokenManager tokenManager)
    {
        _tokenManager = tokenManager;
    }

    public void StartAuthentication()
    {
        var request = HttpContext.Current.Request;
        using (var twitter = new WebConsumer(ServiceDescription, _tokenManager))
        {
            var callBackUrl = new Uri(request.Url.Scheme + "://" + request.Url.Authority + "/Members/TwitterCallback");
            twitter.Channel.Send(
                twitter.PrepareRequestUserAuthorization(callBackUrl, null, null)
            );
        }
    }

    public bool FinishAuthentication()
    {
        using (var twitter = new WebConsumer(ServiceDescription, _tokenManager))
        {
            var accessTokenResponse = twitter.ProcessUserAuthorization();
            if (accessTokenResponse != null)
            {
                UserName = accessTokenResponse.ExtraData["screen_name"];
                return true;
            }
        }

        return false;
    }
}

我在MembersController的构造函数中有以下内容,它使用正确的凭据实例化InMemoryTokenManager

_tokenManager = new InMemoryTokenManager(ConfigUtils.GetAppSetting("TwitterAppId"), ConfigUtils.GetAppSetting("TwitterAppSecret"));

我的两个行动是

    public ActionResult LogonTwitter()
    {
        var client = new TwitterClient(_tokenManager);
        client.StartAuthentication();
        return null;
    }

    public ActionResult TwitterCallback()
    {
        var client = new TwitterClient(_tokenManager);

        if (client.FinishAuthentication())
        {
            return new RedirectResult("/");
        }

        // show error
        return View("LogOn");
    }

错误出现在我的TwitterClient中的 StartAuthentication()中。一旦它调用此行

twitter.Channel.Send(
                twitter.PrepareRequestUserAuthorization(callBackUrl, null, null)
            );

我收到以下错误

Error occurred while sending a direct message or getting the response.
Inner Exception: The remote server returned an error: (401) Unauthorized.

有人有什么建议吗?我昨天和今天早上的大部分时间都在试图对此进行排序。我尝试的所有在线示例似乎也得到401 Unauthorized返回? DotNetOpenAuth和Twitter是否存在已知问题?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:5)

我不记得确切的术语,但你是否在twitter应用程序(以及代码)中设置了一个回调网址?我最近遇到过类似的问题,即使在本地开发我相信你需要设置这个值,即使它只是一个占位符