Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android上的Chrome中打开然后自动关闭OAuth页面?_Android_Xamarin_Google Oauth_Chrome Custom Tabs - Fatal编程技术网

如何在Android上的Chrome中打开然后自动关闭OAuth页面?

如何在Android上的Chrome中打开然后自动关闭OAuth页面?,android,xamarin,google-oauth,chrome-custom-tabs,Android,Xamarin,Google Oauth,Chrome Custom Tabs,[我在Xamarin中这样做,但我怀疑答案并不重要,因为Xamarin或多或少地公开了与本机Java相同的API] 我试图学习OAuth并实现授权流(无隐式授权)。这包括打开浏览器,进行身份验证,然后进行密钥交换。你会认为这真的很容易。下面是我的资料 问题在于,用户登录后,浏览器页面会粘住。如何让它消失? public void Authenticate() { var sb = new StringBuilder(); sb.Append("https://accounts.g

[我在Xamarin中这样做,但我怀疑答案并不重要,因为Xamarin或多或少地公开了与本机Java相同的API]

我试图学习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);
    }
}
public void Authenticate()
{
var sb=新的StringBuilder();
某人附加(”https://accounts.google.com/o/oauth2/v2/auth");
sb.追加(“?客户_id=”);
sb.追加(“&response_type=code”);
sb.Append(&scope=openid%20email”);
sb.Append(“&redirect_uri=:/oauth2redirect”);
var url=sb.ToString();
var uri=Android.Net.uri.Parse(“googlechrome://navigate?url=“+url);
尝试
{
System.Diagnostics.Debug.WriteLine(uri.ToString());
意图i=新意图(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);
}
捕获(例外情况除外)
{
系统.诊断.调试.写入线(例如消息);
}
}
相关的:


我认为你不能强迫chrome关闭,因为它不是你应用程序的一部分。但是,您可以在oauth流之后为回调url注册一个意向过滤器,chrome应将该意向转发回您的应用程序。

您使用的是
chrome
,而不是
chrome自定义选项卡

例如:

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()));
并向
启动模式.SingleTask
活动添加意图过滤器,以捕获重定向并“关闭”共享选项卡。您的身份验证代码将位于Intent数据(
Intent?.data?.ToString()
)中:

我有Google的Java Chrome共享选项卡演示端口和共享库代码的nuget包,可以帮助您实现自己的共享选项卡设置:

  • SushiHangover.Android.Support.CustomTabs.Shared


我有一部分工作,重定向uri返回到我的应用程序,而意图过滤器会将其拾取。从我的应用程序中的意图过滤器/活动中,是否有方法要求Chrome关闭该选项卡?在构建意图时,尝试使用
SingleTask
而不是
SingleTop
[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")]