当前位置: 首页 > 知识库问答 >
问题:

使用身份提供程序对用户进行身份验证的Google API

袁轶
2023-03-14

我还在逐渐习惯Xamarin.Forms,我只是在非常基础的水平上。我为我的问题看了许多文章,但最终还是解决不了。所以...


    private async void GoogleSheetsButton_Tapped()
    {
        string clientId = null;
        string redirectUri = null;

        if (Device.RuntimePlatform == Device.iOS)
        {
            clientId = Constants.iOSClientId;
            redirectUri = Constants.iOSRedirectUrl;
        }
        else if (Device.RuntimePlatform == Device.Android)
        {
            clientId = Constants.AndroidClientId;
            redirectUri = Constants.AndroidRedirectUrl;
        }

        var authenticator = new OAuth2Authenticator(
            clientId,
            null,
            Constants.Scope,
            new Uri(Constants.AuthorizeUrl),
            new Uri(redirectUri),
            new Uri(Constants.AccessTokenUrl),
            null,
            true);

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

        AuthenticationState.Authenticator = authenticator;

        var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
        presenter.Login(authenticator);
    }


    presenter.Login(authenticator);

以下是关于我的源代码的更多信息:

  • 用于客户端ID和URL的“常量”类的源

    public static class Constants
    {
        public static string AppName = "....";

        // OAuth
        // For Google login, configure at https://console.developers.google.com/
        public static string iOSClientId = "6.....apps.googleusercontent.com";
        public static string AndroidClientId = "6.....apps.googleusercontent.com";

        // These values do not need changing
        public static string Scope = "https://www.googleapis.com/auth/userinfo.email";
        public static string AuthorizeUrl = "https://accounts.google.com/o/oauth2/auth";
        public static string AccessTokenUrl = "https://www.googleapis.com/oauth2/v4/token";
        public static string UserInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo";

        // Set these to reversed iOS/Android client ids, with :/oauth2redirect appended
        public static string iOSRedirectUrl = "com.googleusercontent.apps.6......h:/oauth2redirect";
        public static string AndroidRedirectUrl = "com.googleusercontent.apps.6......l:/oauth2redirect";
    }

  • 在身份验证完成/错误时实现的方法的来源,事实上,由于我的错误,我仍然无法命中该方法
 

    async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
    {
        var authenticator = sender as OAuth2Authenticator;
        if (authenticator != null)
        {
            authenticator.Completed -= OnAuthCompleted;
            authenticator.Error -= OnAuthError;
        }

        GoogleLoginUser user = null;
        if (e.IsAuthenticated)
        {
            var request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);
            var response = await request.GetResponseAsync();
            if (response != null)
            {
                string userJson = await response.GetResponseTextAsync();
                user = JsonConvert.DeserializeObject(userJson);
            }

            if (_account != null)
            {
                _store.Delete(_account, Constants.AppName);
            }

            await _store.SaveAsync(_account = e.Account, Constants.AppName);
            await DisplayAlert("Email address", user.Email, "OK");
        }
    }

    void OnAuthError(object sender, AuthenticatorErrorEventArgs e)
    {
        var authenticator = sender as OAuth2Authenticator;
        if (authenticator != null)
        {
            authenticator.Completed -= OnAuthCompleted;
            authenticator.Error -= OnAuthError;
        }

        var message = e.Message;
    }

    null

    public class MainActivity : FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            Forms.Init(this, bundle);
            global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, bundle);

            MobileBarcodeScanner.Initialize(Application);

            LoadApplication(new App());
        }
    }

    null

    [Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
    [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataSchemes = new[] { "com.googleusercontent.apps.6......l" }, DataPath = "/oauth2redirect")]
    public class CustomUrlSchemeInterceptorActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var uri = new Uri(Intent.Data.ToString());

            AuthenticationState.Authenticator.OnPageLoading(uri);

            Finish();
        }
    }

解决方案

  1. 在android project Properties中将android编译器更改为Android7.0。截图
  2. 确保Android Manifest内的目标是SDK版本。截图
  3. 将所有“xamarin.android.*”nuget包更新到最低版本25.4.0.1。最有可能的是目前的23.3.0。我发现更新依赖关系的问题,所以我做手动上传。我去手动下载每个包,并将其移动到包文件夹。然后,我创建了自己的包源,并为路径提供了我的文件夹包,我用它来安装已经下载的NuGet包。截图
  4. 之后我的问题解决了。

共有1个答案

萧波峻
2023-03-14

请将您的Android SDK更新到API24或更高,并将其设置为您项目的编译版本。并将自定义选项卡及其依赖项的引用程序集更新到V25.x.x。就这样。你应该能让它工作。

维杰。

 类似资料:
  • 我是Spring安全的新手,我想用数据库验证用户。我已经用jdbc创建了一个登录页面和一个身份验证提供程序,它检查用户是否存在于数据库中。但是我的代码没有这样做的问题是,它允许所有用户登录!我的代码怎么了?谢谢你的帮助。 这是我的安全会议。xml:

  • 我已经创建了身份提供程序,并且从浏览器中它工作正常。 参考:密钥斗篷身份提供程序后代理登录抛出错误 从浏览器,我可以使用外部IDP登录,如果外部IDP用户不在keycloak中,它会在keyclock中创建,这绝对没问题,并重定向到仪表板。 但我的问题是,我们如何用keycloak rest api实现这个流程? 是否有任何api用于使用外部IDP登录,并将获得外部IDP的令牌以及密钥斗篷的令牌?

  • 我正在尝试使用urllib3连接到网页。代码如下所示。 如果我们假设url是需要使用用户名和密码进行身份验证的某个网页,那么我是否使用正确的代码进行身份验证? 我使用urllib2做这件事很舒服,但使用urllib3做不到同样的事情。 非常感谢

  • jwt不应该仅仅用于认证用户吗?我读到过可以在里面存储非敏感的东西,比如用户ID。将权限级别之类的东西存储在令牌中可以吗?这样我可以避免数据库调用。

  • 在我的Angular 2应用程序中,我希望允许用户通过电子邮件/密码、谷歌和Facebook提供商进行连接。 在第一次登录时,我了解到一个用户条目是在firebase数据库中创建的。例如。如果用户使用另一个提供程序登录,我理解firebase将阻止此操作,特别是如果电子邮件已经被使用并且在规则中设置为unique。 所以我的问题是:如果我们检测到电子邮件是相同的,那么我们如何能够自动的,对用户来说

  • null 我研究了OAuth2隐式授权,但它要求用户在成功验证后批准/拒绝应用程序。它在我的情况下不起作用,因为我同时拥有应用程序和API。 我查看了OAuth2密码授权,它并不完美,因为我需要公开client_id/client_secret。 我关注OAuth2的原因是因为该API最终将是公开的。 忘记OAuth2,在用户发布用户名/密码时手动生成access_token(在本例中,当API公

  • 这是服务到服务身份验证中概述的规则的一个例外吗?该规则规定需要设置为接收服务的url(例如https://xxxxx.run.app)。和是一回事吗? 最后,除了使用googlecloudsdk(即imaging不存在)之外,是否还有其他方法使用用户帐户而不是服务帐户进行身份验证?

  • 问题内容: 我可以通过接收到请求的xml 但不是 没有JavaScript错误,没有跨域策略问题。可能是语法错误,但是我找不到合适的教程。有什么建议吗? 问题答案: 我认为您需要纯格式: