【问题标题】:How to get the authentication access token after Authenticate Users with an Identity Provider?使用身份提供者对用户进行身份验证后如何获取身份验证访问令牌?
【发布时间】:2020-03-03 12:54:58
【问题描述】:

我在一个 xmarine android c# 项目中工作。我想从我的 c# android 应用程序访问 google 联系人。为此,我使用了下面提到的链接: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth 我无法在 OnAuthCompleted 方法中获取 access_token。我该怎么做?

我在下面给出我的代码以便更好地理解。

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

我还根据上面的链接在我的项目中为自定义 URL 方案添加了另一个活动。但它不起作用,代码如下:

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

请帮助我如何在 async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e) 方法中获取 access_token

【问题讨论】:

    标签: c# android xamarin google-api google-oauth


    【解决方案1】:

    首先,请确保您已正确启用设置

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

    请注意:凭据设置,如果您想在 Android 中运行您的应用程序,您应该将应用程序类型设置为 Android。如果您在 google 控制台中编辑 Oauth ID,您可以得到如下截图所示的结果。

    你说CustomUrlSchemeInterceptorActivity不行,难道这个活动没有出现?如果是这样,您可以添加一个布局并删除Finish() 并进行测试。

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

    这是我运行 GIF 的演示。

    我把我的demo上传到了github,大家可以参考一下。

    https://github.com/851265601/OAuthDemo

    【讨论】:

    猜你喜欢
    • 2017-04-11
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2018-11-28
    • 2019-07-20
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多