Xamarin Webauthenticator and Php

315 Views Asked by At

I have almost 4 days with this. I want to use the WebAuthenticator but just can't. It returns "A task was canceled"

This question is for people familiar with Xamarin and WebAuthenticator

Error I get: A task was canceled

Line that crashes: r = await WebAuthenticator.AuthenticateAsync(authUrl, callbackUrl);

Stack: At the end of this post

The code I use is this: https://github.com/xamarin/Essentials/tree/main/Samples/Samples.Android (copy & paste and modify when I see it works)

Also I see this: https://learn.microsoft.com/en-us/xamarin/essentials/web-authenticator?tabs=android

I think my problem is because I don't know the correct way to finish in the backend

My flow is this:

1- App connects via webview to backend to retrieve a Facebook url, something like this:

https://www.facebook.com/v2.6/dialog/oauth?display=popup&response_type=code&client_id=XXXX&redirect_uri=https%3A%2F%2Fid.tinypass.com%2Fid%2Fapi%2Fv1%2Fidentity%2Flogin%2Fsocial%2Fcallback&scope=email&state=XXXX

2- App then connects via Webauthenticator to that url (just as the github code does)

3- User interact and logged in

4- Facebook redirects to my provider (tinypass, piano.io)

5- The provider then calls my backend

6- The backend does some curl stuff

7- Backend redirects to the callback

I'm using Php, and the last code in it is this:

header("location: xamarinessentials://");

Also tried:

header("location: xamarinessentials://#callback");

header("location: xamarinessentials://#access_token=XXXX");

header("location: xamarinessentials://", true, 302);

I have tried this flow manually via the browser in desktop, and all works fine. Of course, it fails because there is no xamarinessentials://, just in the app

I know you need to see the code, and as I said, is the same on github. I downloaded to see how it works and then will integrate to my project with the necessary adjustments

Any idea of what's wrong?

System.Threading.Tasks.TaskCanceledException: A task was canceled.

at Xamarin.Essentials.WebAuthenticator.PlatformAuthenticateAsync (Xamarin.Essentials.WebAuthenticatorOptions webAuthenticatorOptions) [0x001e7] in D:\a\1\s\Xamarin.Essentials\WebAuthenticator\WebAuthenticator.android.cs:96

at LoginSocialPiano.MainPage.webView_Navigated (System.Object sender, Xamarin.Forms.WebNavigatedEventArgs e) [0x0014e] in C:\Users\XXXX\LoginSocialPiano\LoginSocialPiano\MainPage.xaml.cs:63

I'm using VS 2022 17.1.4 - Windows 11 - Real Android device with Chrome 95.0.4638.74

The exact call is in here: https://github.com/xamarin/Essentials/blob/main/Samples/Samples/ViewModel/WebAuthenticatorViewModel.cs#L61

This is the MainActivity.cs file

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Widget;

namespace LoginSocialPiano.Droid
{
    [Activity(Label = "LoginSocialPiano", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        static App formsApp;

        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            Xamarin.Essentials.Platform.Init(this, bundle);
            Xamarin.Forms.Forms.Init(this, bundle);
            Xamarin.Forms.FormsMaterial.Init(this, bundle);

            Xamarin.Essentials.Platform.ActivityStateChanged += Platform_ActivityStateChanged;

            LoadApplication(formsApp ??= new App());
        }

        protected override void OnResume()
        {
            base.OnResume();

            Xamarin.Essentials.Platform.OnResume(this);
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);

            Xamarin.Essentials.Platform.OnNewIntent(intent);
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();

            Xamarin.Essentials.Platform.ActivityStateChanged -= Platform_ActivityStateChanged;
        }

        void Platform_ActivityStateChanged(object sender, Xamarin.Essentials.ActivityStateChangedEventArgs e) =>
            Toast.MakeText(this, e.State.ToString(), ToastLength.Short).Show();

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
    [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = "xamarinessentials")]
    public class WebAuthenticationCallbackActivity : Xamarin.Essentials.WebAuthenticatorCallbackActivity
    {
    }
}
0

There are 0 best solutions below