OAuth2 redirect not launching Xamarin Android application
I have been struggling with integrating Google OAuth2 authentication with our Xamarin application without success. This is one of several samples we have followed.
When the button to authentication via Google is clicked, the application correctly launched the browser and the user os presented with the Google authentication screens. After the user enters in their Google credentials, the local browser navigates to www.google.com instead of relaunching the application.
If I close the browser, the application to continues executing but the authentication response indicates failure.
Here is some sample code from the app:
public class AuthenticationService : IAuthenticationService
{
var authenticator = new OAuth2Authenticator(
clientId: GoogleConfiguration.ClientId,
clientSecret: GoogleConfiguration.ClientSecret,
scope: GoogleConfiguration.Scope,
authorizeUrl: new Uri(GoogleConfiguration.AuthorizeUrl),
redirectUrl: new Uri("com.googleusercontent.apps.my-application-instance-id-12345::/oauth2redirect"),
getUsernameAsync: null,
isUsingNativeUI: GoogleConfiguration.IsUsingNativeUI,
accessTokenUrl: new Uri(GoogleConfiguration.AcessTokenUrl))
{
AllowCancel = true,
ShowErrors = false,
ClearCookiesBeforeLogin = true
};
authenticator.Completed += (sender, args) =>
{
OnAuthCompleted(sender, args);
onAuthenticationComplete.Invoke();
};
authenticator.Error += OnAuthError;
var presenter = new OAuthLoginPresenter();
presenter.Login(_authenticator);
return Task.CompletedTask;
}
Within the Android project, I have defined an IntentFilter on an activity as follows:
[Activity(Label = "GoogleAuthInterceptor", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter(
new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataHosts = new[] {"localhost"},
DataPath = "/oauth2redirect",
DataSchemes = new []
{
"com.googleusercontent.apps.my-application-instance-id-12345" /* google client id reversed*/
},
AutoVerify = true
)]
public class ActivityCustomUrlSchemeInterceptor : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
Logger.Log(this, "on creating Customer Scheme Interceptor");
base.OnCreate(savedInstanceState);
// Create your application here
base.OnCreate(savedInstanceState);
global::Android.Net.Uri uri_android = Intent.Data;
// Convert Android.Net.Url to Uri
var uri = new Uri(uri_android.ToString());
// Close browser
var intent = new Intent(this, typeof(MainActivity));
//intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
StartActivity(intent);
var authenticationService = ViewModelLocator.Resolve<IAuthenticationService>();
authenticationService.OnPageLoading(uri);
this.Finish();
}
}
}
There is no indication of the above Intent being fired from the DeviceLog (logcat) and searching on IntentFilter within the logs returns 0 entries.
I also have attempted running the sample Xamarin Forms app with Google auth and that did not work either. I ran into the exact same issue. Any thoughts?