Xamarin.Auth OAuth Google API for user authentication for Android












2















I have been trying to authenticate via Google OAuth 2.0. Following this 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.



Xamarin Share Library 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;


AuthenticationState Class



public class AuthenticationState
{
public static OAuth2Authenticator Authenticator;
}


The OnAuthCompleted method



private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
if (e.IsAuthenticated)
{
await Navigation.PushAsync(new GoogleLoginSuccess());
}
}


Main Activity 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());
}


CustomUrlSchemeInterceptorActivity



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;
}
}
}


AndroidManifest



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.somenameofapp">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<application android:label="somenameofapp.Android"></application>
<activity android:name="somenameofapp.Android.MainActivity" android:label="MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.companyname.somenameofapp" android:host="com.companyname.somenameofapp" />
</intent-filter>
</activity>
</manifest>


When i close the Custom Tab explicitly i am navigated back to the application with the below toast message.
On Google Auth CustomTabs close message



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










share|improve this question



























    2















    I have been trying to authenticate via Google OAuth 2.0. Following this 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.



    Xamarin Share Library 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;


    AuthenticationState Class



    public class AuthenticationState
    {
    public static OAuth2Authenticator Authenticator;
    }


    The OnAuthCompleted method



    private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
    {
    if (e.IsAuthenticated)
    {
    await Navigation.PushAsync(new GoogleLoginSuccess());
    }
    }


    Main Activity 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());
    }


    CustomUrlSchemeInterceptorActivity



    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;
    }
    }
    }


    AndroidManifest



    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.somenameofapp">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <application android:label="somenameofapp.Android"></application>
    <activity android:name="somenameofapp.Android.MainActivity" android:label="MainActivity">
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="com.companyname.somenameofapp" android:host="com.companyname.somenameofapp" />
    </intent-filter>
    </activity>
    </manifest>


    When i close the Custom Tab explicitly i am navigated back to the application with the below toast message.
    On Google Auth CustomTabs close message



    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










    share|improve this question

























      2












      2








      2








      I have been trying to authenticate via Google OAuth 2.0. Following this 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.



      Xamarin Share Library 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;


      AuthenticationState Class



      public class AuthenticationState
      {
      public static OAuth2Authenticator Authenticator;
      }


      The OnAuthCompleted method



      private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
      {
      if (e.IsAuthenticated)
      {
      await Navigation.PushAsync(new GoogleLoginSuccess());
      }
      }


      Main Activity 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());
      }


      CustomUrlSchemeInterceptorActivity



      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;
      }
      }
      }


      AndroidManifest



      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.somenameofapp">
      <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
      <application android:label="somenameofapp.Android"></application>
      <activity android:name="somenameofapp.Android.MainActivity" android:label="MainActivity">
      <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="com.companyname.somenameofapp" android:host="com.companyname.somenameofapp" />
      </intent-filter>
      </activity>
      </manifest>


      When i close the Custom Tab explicitly i am navigated back to the application with the below toast message.
      On Google Auth CustomTabs close message



      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










      share|improve this question














      I have been trying to authenticate via Google OAuth 2.0. Following this 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.



      Xamarin Share Library 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;


      AuthenticationState Class



      public class AuthenticationState
      {
      public static OAuth2Authenticator Authenticator;
      }


      The OnAuthCompleted method



      private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
      {
      if (e.IsAuthenticated)
      {
      await Navigation.PushAsync(new GoogleLoginSuccess());
      }
      }


      Main Activity 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());
      }


      CustomUrlSchemeInterceptorActivity



      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;
      }
      }
      }


      AndroidManifest



      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.somenameofapp">
      <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
      <application android:label="somenameofapp.Android"></application>
      <activity android:name="somenameofapp.Android.MainActivity" android:label="MainActivity">
      <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="com.companyname.somenameofapp" android:host="com.companyname.somenameofapp" />
      </intent-filter>
      </activity>
      </manifest>


      When i close the Custom Tab explicitly i am navigated back to the application with the below toast message.
      On Google Auth CustomTabs close message



      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







      xamarin xamarin.forms oauth-2.0 xamarin.android google-oauth2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 7:41









      Kinder CappyKinder Cappy

      98118




      98118
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018192%2fxamarin-auth-oauth-google-api-for-user-authentication-for-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018192%2fxamarin-auth-oauth-google-api-for-user-authentication-for-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas