title: Xamarin.Auth OAuth Google API for user authentication for Android
body: <p>I have been trying to authenticate via Google OAuth 2.0. Following <a href="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth" rel="nofollow noreferrer">this</a> link.
  Have been able to open the Google Auth page and login successfully. 
  The problem is after logging in i am redirected to the google search page. 
  If i close the application then in the OnAuthCompleted method of the OAuth2Authenticator i get the e.IsAuthenticated to false and not able to get any information of the user.</p>
  
  <p>Xamarin Share Library code:</p>
  
  <pre><code>var authenticator = new OAuth2Authenticator(
                                  "somekey-somekey1.apps.googleusercontent.com",
                                  null,
                                  "email",
                                  new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
                                  new Uri("com.companyname.somenameofapp:/oauth2redirect"),
                                  new Uri("https://www.googleapis.com/oauth2/v4/"),
                                  null,
                                  true);
          var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
          presenter.Login(authenticator);
          authenticator.Completed += OnAuthCompleted;
          AuthenticationState.Authenticator = authenticator;
  </code></pre>
  
  <p>AuthenticationState Class</p>
  
  <pre><code>public class AuthenticationState
  {
      public static OAuth2Authenticator Authenticator;
  }
  </code></pre>
  
  <p>The OnAuthCompleted method</p>
  
  <pre><code>private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
  {
      if (e.IsAuthenticated)
      {
          await Navigation.PushAsync(new GoogleLoginSuccess());
      }
  }
  </code></pre>
  
  <p>Main Activity Code</p>
  
  <pre><code>protected override void OnCreate(Bundle savedInstanceState)
  {
      TabLayoutResource = Resource.Layout.Tabbar;
      ToolbarResource = Resource.Layout.Toolbar;
      base.OnCreate(savedInstanceState);
      global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
      global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, savedInstanceState);
      LoadApplication(new App());
  }
  </code></pre>
  
  <p>CustomUrlSchemeInterceptorActivity</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  
  namespace XamAppGoogleAuth.Droid
  {
      [Activity(Label = "CustomUrlSchemeInterceptorActivity")]
      [IntentFilter(
          new[] { Intent.ActionView },
          Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
          DataSchemes = new[] { "com.companyname.somenameofapp" },
          DataPath = ":/oauth2redirect",
          DataHost = "com.companyname.somenameofapp")]
      public class CustomUrlSchemeInterceptorActivity : Activity
      {
          protected override void OnCreate(Bundle savedInstanceState)
          {
              base.OnCreate(savedInstanceState);
              global::Android.Net.Uri uri_android = Intent.Data;
  
              Uri uri_netfx = new Uri(uri_android.ToString());
  
              // load redirect_url Page
              AuthenticationState.Authenticator.OnPageLoading(uri_netfx);
  
              var intent = new Intent(this, typeof(MainActivity));
              intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
              StartActivity(intent);
  
              this.Finish();
  
              return;
          }
      }
  }
  </code></pre>
  
  <p>AndroidManifest</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.somenameofapp"&gt;
    &lt;uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" /&gt;
    &lt;application android:label="somenameofapp.Android"&gt;&lt;/application&gt;
    &lt;activity android:name="somenameofapp.Android.MainActivity" android:label="MainActivity"&gt;
      &lt;intent-filter&gt;
        &lt;action android:name="android.intent.action.VIEW" /&gt;
        &lt;category android:name="android.intent.category.DEFAULT" /&gt;
        &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
        &lt;data android:scheme="com.companyname.somenameofapp" android:host="com.companyname.somenameofapp" /&gt;
      &lt;/intent-filter&gt;
    &lt;/activity&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>When i close the Custom Tab explicitly i am navigated back to the application with the below toast message.
  <a href="https://i.stack.imgur.com/T4TGU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T4TGU.png" alt="On Google Auth CustomTabs close message"></a></p>
  
  <p>What i would want is after the user authenticates with Google OAuth, they are redirected back to the application and I can get the access token and other information as needed. I have been to a lot of links regarding this but havent been able to find a solution. Kindly help</p>