安卓应用在登录后无法恢复



我试图通过谷歌实现OAuth在我的xamarin应用程序。我的代码看起来像:

var auth = new OAuth2Authenticator
(
clientId: clientId,
scope: scope,
authorizeUrl: new Uri(oauthUrl),
redirectUrl: new Uri(redirectUrl),
clientSecret: clientSecret,
accessTokenUrl: new Uri(accessTokenUrl), 
getUsernameAsync: null, 
isUsingNativeUI:true
);
auth.Completed += AuthOnCompleted;
auth.Error += AuthOnError;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(auth);

private async void AuthOnCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
//breakpoint, which never reach
}
private async void AuthOnError(object sender, AuthenticatorErrorEventArgs e) 
{
//breakpoint, which never reach
}

在重定向之前,流是正确的。

redirectUrl = https://accounts.google.com/o/oauth2/token

成功登录后,我得到了这样的url:

https://accounts.google.com/o/oauth2/token?state=ojgjqhcgyidimcec&代码= 4/0AX4XfwjvhStjH5RAAzLyXCu_dTPvPyZ_eee-gHqKZoglVJ-7PCR6HDkPAo9mfEMYnWdjyA&范围= https://www.googleapis.com/auth/youtube.readonly

很明显,我们已经获得了用户令牌,但应用程序没有进入下一步,到AuthOnCompleted方法。App只是停留在重定向URL上。如何关闭浏览器并返回应用程序进入AuthOnCompleted?

乌利希期刊指南

MainActivity.cs:

[Activity(Label = "MyApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation |
ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
[IntentFilter(
actions: new[] { Intent.ActionView }, 
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[]
{
"com.googleusercontent.apps.Project ID from https://console.cloud.google.com/home/dashboard?project=my_proj",
},
DataPaths = new[]
{
"/oauth2redirect",
})]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

好的,我在这里找到了一个有效的解决方案https://github.com/TimLariviere/Sample-XamarinAuth-Google

我的错误是我把DataSchemes写成com.googleusercontent.apps.Project ID,但正确的是com.companyname.testapp包名

相关内容

  • 没有找到相关文章

最新更新