OAuth with Xamarin Forms for Google Authentication

JKZulu ZULU 1 Reputation point
2022-02-19T14:57:37.087+00:00

Hello,

I'm a bit helpless. I implement an OAuth authentication for Google in my Xamarin Form App. I read a lot of examples and saw a lot of Youtube videos.
I did all the steps on google developer and in VS2022 what I had to do. But in every case I get the following error.

176103-google.jpg

my code look like this:

 private void ConnectGoogle()  
        {  
            string clientId = null;  
            string redirectUri = null;  
             
  
            switch (Device.RuntimePlatform)  
            {  
                case Device.Android:  
                    clientId = Constants.AndroidClientId;  
                    redirectUri = Constants.AndroidRedirectUrl;  
                    break;  
            }  
  
            account = store.FindAccountsForService(Constants.AppName).FirstOrDefault();  
  
  
            //Definition OAUth2Auth.....  
            //public OAuth2Authenticator(string clientId,  
            //string clientSecret,  
            //string scope,  
            //Uri authorizeUrl,  
            //Uri redirectUrl,  
            //Uri accessTokenUrl,  
            //GetUsernameAsyncFunc  
            //getUsernameAsync = null,  
            //bool isUsingNativeUI = false);  
  
            var authenticator = new OAuth2Authenticator(  
                clientId,  
                null,  
                Constants.Scope,  
                new Uri(Constants.AuthorizeUrl),  
                new Uri(redirectUri),  
                new Uri(Constants.AccessTokenUrl),  
                null,  
                false);  
  
             
            authenticator.Completed += OnAuthCompleted;  
            authenticator.Error += OnAuthError;  
  
            AuthenticationState.Authenticator = authenticator;  
  
            var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();  
            presenter.Login(authenticator);  
        }  

I have no idea where my error is. Hopefully anyone has an tip for me.

Best regards
Juergen

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-02-21T03:15:13.137+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Error 403, or disallowed_useragent and a Google error

    Google made an annoucement saying that they will block any OAuth in webviews, do you open the auth page in your application's webview?

    If yes, Please set the IsUsingNativeUI flag to true when you new OAuth2Authenticator() like following code( I notice you set the IsUsingNativeUI to false). And install the latest Chrome and set Chrome as the default browser.

       var authenticator = new OAuth2Authenticator(  
                        clientId,  
                        null,  
                        Constants.Scope,  
                        new Uri(Constants.AuthorizeUrl),  
                        new Uri(redirectUri),  
                        new Uri(Constants.AccessTokenUrl),  
                        null,  
                        true);  
    

    And If you use Android 11. please refer to this thread, add two intents in your AndroidManifest.xml file:

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.