1

I want to let user connect to my Xamarin Forms app with their Google account. I have been following this tutorial: https://www.syncfusion.com/blogs/post/google-login-integration-in-xamarin-forms-a-complete-guide.aspx

What should go into redirectUrl value? Now I have '400 error: invalid_request'. I probably should register that page somewhere? And what that page should be? I would like to get user info after login and redirect them to main app page (HomePage()).

I have this code:

    Xamarin.Auth.OAuth2Authenticator authenticator = null;
    private void SignInGoogleClicked(object sender, EventArgs e)
    {
        authenticator
            = new Xamarin.Auth.OAuth2Authenticator
            (
                clientId: "498923533154-uefrhbk3i9v7nqd7lep9lqb85u783m7v.apps.googleusercontent.com",
                scope: "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/cloud-healthcare",
                authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"),
                redirectUrl:
                new Func<Uri>
                (
                    () =>
                    {
                        string uri = null;
                        switch (Xamarin.Forms.Device.RuntimePlatform)
                        {
                            case "Android":
                                uri =
                                    "com.xamarin.traditional.standard.samples.oauth.providers.android:/oauth2redirect"
                                    //"com.googleusercontent.apps.1093596514437-uefrhbk3i9v7nqd7lep9lqb85u783m7v:/oauth2redirect"
                                    ;
                                break; 
                            case "iOS":
                                uri =
                                    "com.xamarin.traditional.standard.samples.oauth.providers.ios:/oauth2redirect"
                                    //"com.googleusercontent.apps.1093596514437-cajdhnien8cpenof8rrdlphdrboo56jh:/oauth2redirect"
                                    ;
                                break;
                            case "Windows":
                                uri =
                                    "com.xamarin.traditional.standard.samples.oauth.providers.ios:/oauth2redirect"
                                    //"com.googleusercontent.apps.1093596514437-cajdhnien8cpenof8rrdlphdrboo56jh:/oauth2redirect"
                                    ;
                                break;
                        }

                        return new Uri(uri);
                    }
                ).Invoke(),
                getUsernameAsync: null,
                isUsingNativeUI: false
            )
            {
                AllowCancel = true,
            };

        NavigateLoginPage();
    }

    Xamarin.Auth.XamarinForms.AuthenticatorPage login_page = null;

    private void NavigateLoginPage()
    {
        login_page = new Xamarin.Auth.XamarinForms.AuthenticatorPage()
        {
            Authenticator = authenticator,
        };
        Navigation.PushAsync(login_page);
    }
vytaute
  • 1,260
  • 4
  • 16
  • 36

1 Answers1

1

You could try set <your app name>:/oauth2redirect as rediction url, such like this: com.companyname.appname:/oauth2redirect.

Also, we may implement Google authentication in the Android and iOS project separately.

For more info, you could refer to Authenticate users through Google with Xamarin.Auth.

Liqun Shen-MSFT
  • 3,490
  • 2
  • 3
  • 11
  • Unfortunately, I got error Custom scheme URI's are not allowed for WEB client type – vytaute Apr 04 '23 at 14:53
  • I am probably using wrong type, because I used web client type for mobile app. – vytaute Apr 04 '23 at 14:55
  • Find an issue which might be helpful: [Custom scheme URIs are not allowed for 'Web' client type - Google with Firebase](https://stackoverflow.com/questions/43060912/custom-scheme-uris-are-not-allowed-for-web-client-type-google-with-firebase) – Liqun Shen-MSFT Apr 05 '23 at 01:30