Login with twitter using Xamarin.Auth throwing Authorization Required (401) Exception.

Vai1212Vai1212 Member ✭✭
edited July 2018 in Xamarin.Forms

Hi All,
I am working on user login with twitter. I found some code and implemented in my Xamarin forms application and i also created consume key and other setting as well on twitter . After Running my application and hitting the Login with Twitter i am getting Exception : The Remote Server Error:401 Authorization Required. Please help me , i am struggling since last 2 days.
Here is my Code:

        OAuth1Authenticator Twitterauth = null;
        try
        {
            Twitterauth = new OAuth1Authenticator(
                       consumerKey: "XXXXXX", 
                       consumerSecret:"XXXXXXXXXX", 
                       requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"), 
                       authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"), 
                       accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
                       callbackUrl: new Uri("http://twitter.com")  
                   );
        }
        catch(Exception ex)
        {

        }

                    auth.Completed += (sender, eventArgs) =>
                    {
                        if (eventArgs.IsAuthenticated)
                        {
                            OAuthConfig.User = new UserDetails();
                        // Get and Save User Details 
                        OAuthConfig.User.Token = eventArgs.Account.Properties["oauth_token"];
                            OAuthConfig.User.TokenSecret = eventArgs.Account.Properties["oauth_token_secret"];
                            OAuthConfig.User.TwitterId = eventArgs.Account.Properties["user_id"];
                            OAuthConfig.User.ScreenName = eventArgs.Account.Properties["screen_name"];

                            OAuthConfig.SuccessfulLoginAction.Invoke();
                        }
                        else
                        {
                        // The user cancelled
                    }
                    };


                    activity.StartActivity(auth.GetUI(activity));
Tagged:

Answers

  • ElvisXiaElvisXia Member, Forum Administrator, Xamarin Team Xamurai

    From the Source Codes of Xamarin.Auth. It is generating a Get Request . But according to the Official document of twitter Auth1. It requires a post request and a bunch of header params are necessary for the header. So obviously Xamarin.Auth is not a good fit here.

    The fastest way to do authentication would be using other library like Tweetinvi to do the job. Alternatively you can also use HttpClient or HttpRequestMessage to generate a valid Http Post Request.

  • JohnHardmanJohnHardman GBUniversity mod

    @moljac - What's the current state of Twitter support in Xamarin.Auth? Does it/should it support the case above?

Sign In or Register to comment.