0

I have scoured the web trying to get an answer for this and I have found many but none of them work. Google requires that the authentication happens in the browser and not in a WebView. I have implemented it like this for Android:

var activity = this.Context as Activity;

var auth = new OAuth2Authenticator(
    clientId: "xxxx.apps.googleusercontent.com",
    scope: "",
    authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"),
    redirectUrl: new Uri("myredirecturi"));
auth.Completed += (sender, eventArgs) => {
    if (eventArgs.IsAuthenticated)
        {
            App.SuccessfulLoginAction.Invoke();
            App.SaveToken(eventArgs.Account.Properties["access_token"]);
        }
};
activity.StartActivity(auth.GetUI(activity));

And like this for iOS:

var auth = new OAuth2Authenticator(
    clientId: "xxxx.apps.googleusercontent.com", // your OAuth2 client id
    scope: "",
    authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"),
    redirectUrl: new Uri("myredirecturi"));

auth.Completed += (sender, eventArgs) =>
{
    App.SuccessfulLoginAction.Invoke();

    if (eventArgs.IsAuthenticated)
    {
        App.SaveToken(eventArgs.Account.Properties["access_token"]);
    }
    PresentViewController(auth.GetUI(), true, null);
};

But this does exactly what I don't want it to do (opens a WebView) and every single answer I have found does exactly this. How do I go about doing it correctly so that the browser opens and then returns to my app when it is done?

Furthermore, I know the scope specifies what I want to do, and in this case it is just a sign-in, but I have yet to find an example of what to actually put in there.

Any example will be really appreciated.

  • I have found this similar question [link](https://stackoverflow.com/questions/45750554/xamarin-forms-google-authenticateion-error-disallowed-useragent?rq=1) and the answer states that if it is running on an emulator without Chrome installed it will revert back to WebView. This is what I was doing, but I debugged on my phone and got the exact same issue. – David van Rensburg Oct 10 '17 at 17:52

1 Answers1

0

You need to specifically enable the native UI type of authentication. So add the following to your OAuth2Authenticators in each project:

new OAuth2Authenticator {
    isUsingNativeUI = true
}

Also make sure you are using at least version 1.5.0+ to get this property.

hvaughan3
  • 10,955
  • 5
  • 56
  • 76