【问题标题】:Xamarin.Auth : Authenticating Users with an Identity Provider [xamarin forms - Android]Xamarin.Auth:使用身份提供者对用户进行身份验证 [xamarin forms - Android]
【发布时间】:2018-03-08 08:56:51
【问题描述】:

我正在关注https://developer.xamarin.com/guides/xamarin-forms/cloud-services/authentication/oauth/ 的这个示例教程

下载示例后,我按照说明更改了所有 clientID 端点。我进入了 Google 登录页面,浏览器设法关闭。浏览器关闭后,它总是进入 OnAuthError 并且错误消息是“Error authentication : invalid_request”

我无法触发 OnAuthCompleted。它总是进入 OnAuthError。

 void OnLoginClicked(object sender, EventArgs e)
        {
            string clientId = null;
            string redirectUri = null;

            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    clientId = Constants.iOSClientId;
                    redirectUri = Constants.iOSRedirectUrl;
                    break;

                case Device.Android:
                    clientId = Constants.AndroidClientId;
                    redirectUri = Constants.AndroidRedirectUrl;
                    break;
            }

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

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

            User user = null;
            if (e.IsAuthenticated)
            {
                // If the user is authenticated, request their basic user data from Google
                // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
                var request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);
                var response = await request.GetResponseAsync();
                if (response != null)
                {
                    // Deserialize the data and store it in the account store
                    // The users email address will be used to identify data in SimpleDB
                    string userJson = await response.GetResponseTextAsync();
                    user = JsonConvert.DeserializeObject<User>(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;
            }

            Debug.WriteLine("Authentication error: " + e.Message);
        }

我已阅读所有相关链接。我从 SO 得到的最接近的问题是:Xamarin.Forms Google API Authenticating Users with an Identity Provider 但在配置设置并更新我的包后它仍然失败。我正在使用 Xamarin.Auth 1.5.0.3(最新稳定版本)

任何真正的英雄都可以运行本教程并使其发挥作用?

【问题讨论】:

    标签: android xamarin.auth


    【解决方案1】:

    问题是我在 Google 控制台开发者网站上使用 Type : "Others"。当我切换回类型时:“Android”。它正在工作。

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2018-01-22
      • 1970-01-01
      • 2014-09-11
      • 2019-07-20
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 2013-09-13
      相关资源
      最近更新 更多