Home > Blockchain >  Android App doesn't get back after logging
Android App doesn't get back after logging

Time:04-26

I'm trying to implement OAuth through google in my xamarin app. My code looks like :

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
}

The flow goes correct until redirection.

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

After successfully logging I got to url kind of this :

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

It's clear that we have got user token, but app doesn't go next step, to AuthOnCompleted method. App just staying on redirect URL. How to close browser and get back to app into 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);
}

CodePudding user response:

Ok I found a working solution here https://github.com/TimLariviere/Sample-XamarinAuth-Google

My mistake was I wrote DataSchemes as com.googleusercontent.apps.Project ID but correct is com.companyname.testapp package name

  • Related