Xamarin.Auth:使用身份提供程序验证用户[xamarin表单 - Android]

时间:2017-09-27 01:47:06

标签: android xamarin.auth

我正在关注https://developer.xamarin.com/guides/xamarin-forms/cloud-services/authentication/oauth/

的示例教程

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

error message

我无法解雇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(最新稳定版)

外面有真正的英雄可以运行本教程并使其有效吗?

1 个答案:

答案 0 :(得分:0)

问题是我使用Type:&#34; Others&#34;在Google控制台开发者网站上。当我切换回类型:&#34; Android&#34;。它正在工作。 Google Console