[I'm doing this in Xamarin, but I suspect the answer doesn't matter about that much as Xamarin exposes more or less the same API as the native Java]
I'm trying to learn OAuth and implement the Authorization Flow (no Implicit Grant). This involves opening the browser, doing the authentication and then not doing the key-exchange. You'd think this would be really easy. Here's what I have below.
The problem with this is that the browser page sticks around after the user logs in. How do I get it to go away?
public void Authenticate()
{
var sb = new StringBuilder();
sb.Append("https://accounts.google.com/o/oauth2/v2/auth");
sb.Append("?client_id=<MY ID HERE>");
sb.Append("&response_type=code");
sb.Append("&scope=openid%20email");
sb.Append("&redirect_uri=<MY PACKAGE NAME HERE>:/oauth2redirect");
var url = sb.ToString();
var uri = Android.Net.Uri.Parse("googlechrome://navigate?url=" + url);
try
{
System.Diagnostics.Debug.WriteLine(uri.ToString());
Intent i = new Intent(Intent.ActionView, uri);
i.AddFlags(ActivityFlags.NewTask);
i.AddFlags(ActivityFlags.SingleTop);
i.AddFlags(ActivityFlags.ClearTop);
i.AddFlags(ActivityFlags.NoHistory);
Android.App.Application.Context.StartActivity(i);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
Related:
Is there any way in Android to force open a link to open in Chrome?
Redirect page doesn't automatically close after successful OAuth authorization
You are using
Chrome
, not aChrome Custom Tab
:Example:
And adding an Intent filter to a
LaunchMode.SingleTask
Activity to catch the redirect and "close" theShared Tab
. Your auth code will be in the Intent data (Intent?.Data?.ToString()
):I have port of Google's Java Chrome Shared Tab demo and a nuget package of their shared library code to aid in the implementation of your own Shared Tab setup:
SushiHangover.Android.Support.CustomTabs.Shared
https://github.com/sushihangover/SushiHangover.GoogleChrome.CustomTabs.Shared