Android 登录后应用无法返回

Android App doesn't get back after logging

我正在尝试通过 google 在我的 xamarin 应用程序中实施 OAuth。我的代码看起来像:

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&code=4/0AX4XfwjvhStjH5RAAzLyXCu_dTPvPyZ_eee-gHqKZoglVJ-7PCR6HDkPAo9mfEMYnWdjyA&scope=https://www.googleapis.com/auth/youtube.readonly

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

UPD

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 包名