Xamarin.Auth for twitter can't be canceled

Hi everyone,

I am stuck with a weird performance from Xamarin.Auth for twitter api.

I tried to use the following helper and get the result back whether the user is authenticated or canceled it. I called this helper method from my original activity and then start twitter authenticate activity.

            var account = await AuthHelper.OAuth1(this,
                //Constants.TwitterConsumerKey,
                //Constants.TwitterConsumerSecret,
                TwitterAppId,TwitterAppSecret,
                new Uri("https://api.twitter.com/oauth/request_token"),
                new Uri("https://api.twitter.com/oauth/authorize"),
                new Uri("https://api.twitter.com/oauth/access_token"),
                new Uri("http://twitter.com"));



static class AuthHelper
{
    public static Task<Account>OAuth1( Activity ac,
        string consumerKey, string consumerSecret, 
        Uri requestTokenUrl, Uri authorizeUrl, 
        Uri accessTokenUrl, Uri callbackUrl)
    {
        TaskCompletionSource<Account> tcs = 
            new TaskCompletionSource<Account>();

        var auth = new OAuth1Authenticator(
            consumerKey, consumerSecret, requestTokenUrl,
            authorizeUrl, accessTokenUrl, callbackUrl);

        auth.AllowCancel = true;
        auth.Completed += (sender, e) =>
        {
            tcs.SetResult(e.IsAuthenticated ? 
                e.Account : null);
        };

        var ui = auth.GetUI(ac);
        ac.StartActivity(ui);

        return tcs.Task;
    }
}

But it doesn't work when the user clicked cancel button, it could not go back to the original activity which start twitter authenticate activity, but get back to the twitter login page as the image I attached. Please let me know if you get same issue and have any idea what was wrong. Thanks in advance!

Answers

  • IsaacCorralesRuizIsaacCorralesRuiz MXMember ✭✭

    I have the same problem on IOS

  • SylvainLevySylvainLevy FRMember

    I have the same issue with the same example.

    It seems that Xamarin.Auth use WebView for login process.
    And after the login process or cancel, it redirect you to the redirect URL paramaters.
    In our case "www.twitter.com", the only way to comeback to the Activity is to press KeyBack.

    Is there a mean to build an URL for redirecting the Webview to the app ???

  • DavidOliverDavidOliver USUniversity

    For anyone else who has this issue: there is an Error event on the Authenticator that will be called when the user cancels giving their credentials.

    An easy way to return from the activity when the user cancels is to assign the authenticator object's own OnCancelled method:
    auth.Error += (sender, e) => auth.OnCancelled();

Sign In or Register to comment.