我正在尝试从Android应用程序中的0auth2获取身份验证令牌。问题是我签名后,由于重定向URL,应用程序正在重新启动,但是一旦重新启动应用程序,我就无法从意图中获取任何身份验证令牌。
code
var authenticator = new OAuth2Authenticator(ClientID, ClientSecret, Scope, new Uri("https://accounts.google.com/o/oauth2/auth"),
new Uri("com.app.abcd:/oauthredirect"), new Uri("https://oauth2.googleapis.com/token"), null, true);
authenticator.Completed += OnAuthCompleted;
authenticator.Error += OnAuthError;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(authenticator);
private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
var authenticator = sender as OAuth2Authenticator;
if (authenticator != null)
{
authenticator.Completed -= OnAuthCompleted;
authenticator.Error -= OnAuthError;
}
IsAuthorized = e.IsAuthenticated;
//User user = null;
if (e.IsAuthenticated)
{}
}
Android
[Activity(Label = "ABCD", MainLauncher = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize, ScreenOrientation = ScreenOrientation.Portrait)]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
//DataScheme = "abcd",
DataSchemes = new[] { "com.app.abcd" },
DataPath = "/oauthredirect",
AutoVerify = true)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
DependencyService.Register<ChromeCustomTabsBrowser>();
CrossCurrentActivity.Current.Activity = this;
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
//HandleNotificationIntent(intent);
}
protected override void OnRestart()
{
var intent = this.Intent;
base.OnRestart();
}
}
[当我运行此代码并调用presenter.Login(authenticator);
时,我可以看到google对用户进行身份验证,并且调用了OnNewIntent
,应用重新启动,并且从未调用OnAuthCompleted
,并且我无法获取访问令牌。] >
这是从0auth2获取访问令牌的正确方法,还是我缺少什么?谁能帮我这个忙。
我正在尝试从Android应用程序中的0auth2获取身份验证令牌。问题是我签名后,由于重定向网址,应用程序正在重新启动,但是一旦...
我找到了解决方案。查看示例here。