I am trying to get auth token from 0auth2 in the android app. The issue is once i sign, in the app is restaring because of the redirect url but I am not getting any auth token from the intent once the app is restarted.
code
var authenticator = new OAuth2Authenticator(ClientID, ClientSecret, Scope, new Uri("https://accounts.google.com/o/oauth2/auth"),
new Uri("com.app.abcd:/oauthredirect"), new Uri("https://oauth2.googleapis.com/token"), null, true);
authenticator.Completed += OnAuthCompleted;
authenticator.Error += OnAuthError;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(authenticator);
private async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
var authenticator = sender as OAuth2Authenticator;
if (authenticator != null)
{
authenticator.Completed -= OnAuthCompleted;
authenticator.Error -= OnAuthError;
}
IsAuthorized = e.IsAuthenticated;
//User user = null;
if (e.IsAuthenticated)
{}
}
Android
[Activity(Label = "ABCD", MainLauncher = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize, ScreenOrientation = ScreenOrientation.Portrait)]
[IntentFilter(new[] { Intent.ActionView },
Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
//DataScheme = "abcd",
DataSchemes = new[] { "com.app.abcd" },
DataPath = "/oauthredirect",
AutoVerify = true)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
DependencyService.Register<ChromeCustomTabsBrowser>();
CrossCurrentActivity.Current.Activity = this;
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
//HandleNotificationIntent(intent);
}
protected override void OnRestart()
{
var intent = this.Intent;
base.OnRestart();
}
}
When i run this code and presenter.Login(authenticator);is called i can see the google authenticates the user and OnNewIntent is called and app is restarted and OnAuthCompleted is never called and I am not able to get the access token.
Is this the right way to get accesstoken form 0auth2 or am i missing something? can anyone help me with this.
I found the solution. Check out the example here.
Common Code:
ANDROID: Create a new activity:
iOS
Add couple of lines in
OpenUrlto handle the url redirectIn info.plist add the urlscheme of the package name: