【发布时间】:2020-04-21 13:41:12
【问题描述】:
我正在尝试从 android 应用程序中的 0auth2 获取身份验证令牌。问题是,一旦我签名,由于重定向 url,应用程序正在重新启动,但是一旦应用程序重新启动,我就没有从意图中获得任何身份验证令牌。
代码
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)
{}
}
安卓
[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 获取访问令牌的正确方法还是我遗漏了什么?谁能帮我解决这个问题。
【问题讨论】:
-
github.com/xamarin/Xamarin.Auth/tree/master/samples/… 这个示例真的很有帮助。它帮助我解决了问题。
-
很高兴听到您自己解决了您的问题,请在这里分享您的解决方案,并将您的回复标记为答案,对遇到同样问题的人非常有帮助,谢谢。跨度>
标签: android xamarin oauth-2.0 xamarin.android xamarin.auth