How to get the authentication access token after Authenticate Users with an Identity Provider?

Viewed 284

I am working in a xmarine android c# project. where I want to access google contact from my c# android app. For This I have used the below mentioned link: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth I am unable to get access_token in OnAuthCompleted method. How can I do so?

I am giving my code below for better understanding.

public void googleAuthentication()
        {
            var authenticator = new OAuth2Authenticator(
                                Configuration.ClientID,
                                string.Empty,
                                "https://www.googleapis.com/auth/contacts",
                                new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
                                new Uri(Configuration.RedirectUrl),
                                new Uri("https://www.googleapis.com/oauth2/v4/token"),
                                isUsingNativeUI: true);

            authenticator.Completed += OnAuthCompleted;
            authenticator.Error += Authenticator_Error;

            var a = authenticator.GetUI(this);
            StartActivity(a);            
        }

also I have added another activity in my project for custom URL scheme as per the above link. but it is not working, code is as follows:

[Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
    [IntentFilter(
        new[] { Intent.ActionView },
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
        DataSchemes = new[] { "com.googleusercontent.apps.94221532031-00o4meh3gmmq4g8r3ersa6m3oskmbkat" },
        DataPath = "/oauth2redirect")]
    public class CustomUrlSchemeInterceptorActivity : Activity
    {
        public static OAuth2Authenticator Auth;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Create your application here
            var uri = new Uri(Intent.Data.ToString());
            // Load redirectUrl page            
            CustomUrlSchemeInterceptorActivity.Auth.OnPageLoading(uri);
            Finish();
        }
    }

please help me how to get access_token in async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e) method

1 Answers

First of all, please make sure you have enable the settings correctly

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth#setup

Please note:The credentials settings, If you want to running your application in Android, you should set the Application type to Android. If you edit the Oauth ID in google console, you can got the resule like following screenshot.

enter image description here

You said the CustomUrlSchemeInterceptorActivity not work, Is this activity did not appear? If so, you can add a Layout and delete the Finish() and make a test.

    [Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true, 
    LaunchMode = LaunchMode.SingleTop)]
    [IntentFilter(
    new[] { Intent.ActionView },
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataSchemes = new[] { "com.googleusercontent.apps.991626672383-b159bdtjq2pt29r7hrm90qi7l3qet99j" },
    DataPath = "/oauth2redirect")]
public class CustomUrlSchemeInterceptorActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.layout1);
        // Convert Android.Net.Url to Uri
        var uri = new Uri(Intent.Data.ToString());

        // Load redirectUrl page
        AuthenticationState.Authenticator.OnPageLoading(uri);

        //Finish();
    }
}

Here is my demo running GIF.

enter image description here

I upload my demo to github, you can refer to it.

https://github.com/851265601/OAuthDemo

Related