I'm starting to tear my hair out with Twitter and trying to signin a user!!! I have Facebook, Google, OpenId all working fine, just Twitter being a PAIN.
I am constantly getting 401 Unauthorized when I try to run my code and for the life of me cannot figure out why.
I have created a twitter client and and I'm using it with the InMemoryTokenManager from the DotNetOpenAuth sample 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 the following in the constructor of my MembersController which is instantiating the InMemoryTokenManager with the correct credentials
_tokenManager = new InMemoryTokenManager(ConfigUtils.GetAppSetting("TwitterAppId"), ConfigUtils.GetAppSetting("TwitterAppSecret"));
And my two Actions are
    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 the StartAuthentication() in my TwitterClient. As soon as it 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.
Anyone got any advice? I have spent most of yesterday and this morning trying to sort this. All the online examples I have tried also seem to get 401 Unauthorized back? Is there a known issue with DotNetOpenAuth and Twitter?
Any help very much appreciated.
I can't remember the exact terminology but have you set up a callback URL in the twitter app (as well as in the code)? I've had similar problems recently, even when developing locally I believe you need to set that value, even if its just a placeholder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With