2017-09-14 104 views
1

[我在Xamarin中這樣做,但我懷疑答案並不重要,因爲Xamarin公開的API與本地Java差不多,授權流程(無隱含授權)。這需要打開瀏覽器,做身份驗證,然後做密鑰交換。你會認爲這很容易。這是我下面的內容。如何在Android版Chrome中打開並自動關閉OAuth頁面?

這樣做的問題是,瀏覽器頁面在用戶登錄後支左右。我如何讓它消失?

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); 
    } 
} 

相關:

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

回答

0

我不認爲你可以強制鉻關閉,因爲它不是你的應用程序的一部分。但是,您可以基於OAuth的認證流程後註冊回調URL的意圖過濾器和鍍鉻應該轉發意圖回到您的應用程序。

+0

我有部分工作,其中REDIRECT_URI回來我的應用程序和意圖過濾撿起來。從應用中的意圖過濾器/活動回來,有沒有辦法讓Chrome關閉該選項卡? – 010110110101

+0

構建您的意圖時,請嘗試使用'SingleTask'而不是'SingleTop' – Jon

2

您使用Chrome,而不是一個Chrome Custom Tab

例子:

var sb = new StringBuilder() 
    .Append("https://accounts.google.com/o/oauth2/v2/auth") 
    .Append($"?client_id={clientID}") 
    .Append("&response_type=code") 
    .Append("&scope=https://www.googleapis.com/auth/drive") 
    .Append($"&redirect_uri={PackageName}:/SushiRedirect"); 

var builder = new CustomTabsIntent.Builder(GetSession()) 
    .SetToolbarColor(Color.ParseColor(TOOLBAR_COLOR)).SetShowTitle(true) 
    .SetStartAnimations(this, Resource.Animation.slide_in_right, Resource.Animation.slide_out_left) 
    .SetExitAnimations(this, Resource.Animation.slide_in_left, Resource.Animation.slide_out_right) 
    .SetCloseButtonIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_arrow_back)); 
var customTabsIntent = builder.Build(); 
CustomTabsHelper.AddKeepAliveExtra(this, customTabsIntent.Intent); 
customTabsIntent.LaunchUrl(this, Uri.Parse(sb.ToString())); 

並添加一個Intent過濾器的LaunchMode.SingleTask活動趕重定向和 「關閉」 Shared Tab。您的授權碼將在意向書數據(Intent?.Data?.ToString()):

[Activity(Label = "CustomTabsClient.Application", MainLauncher = true, Icon = "@drawable/ic_launcher", LaunchMode = LaunchMode.SingleTask)] 
[IntentFilter(
    new[] { Intent.ActionView }, 
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
    DataScheme = "com.sushhangover.customtabsclient.example", DataPath = "/SushiRedirect")] 

我有谷歌的Java瀏覽器共享選項卡演示的端口和NuGet包他們的共享庫的代碼在自己的共享選項卡設置的實施,以幫助:

相關問題