DotNetOpenAuth Twitter Authentication Return 401 Unauthorized

I'm starting to rip my hair off on Twitter and trying to sign up a user! I have Facebook, Google, OpenId everything works fine, just Twitter is PAIN.

I constantly get 401 Unauthorized when I try to run my code, and for life I can not understand why.

I created a twitter client and I use it with the InMemoryTokenManager from the DotNetOpenAuth solution. My twitter client is here

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;
    }
}

And I have in my MemberController constructor that creates an InMemoryTokenManager instance with the correct credentials

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

And my two actions:

    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");
    }

The error appears in StartAuthentication () in my TwitterClient. As soon as he calls this line

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

I get the following error

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?

.

+5
1

, URL- Twitter ( )? , , , ,

+5

All Articles