Xamarin.Auth:使用身份提供者对用户进行身份验证 [xamarin forms - Android]
Posted
技术标签:
【中文标题】Xamarin.Auth:使用身份提供者对用户进行身份验证 [xamarin forms - Android]【英文标题】:Xamarin.Auth : Authenticating Users with an Identity Provider [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(最新稳定版本)
任何真正的英雄都可以运行本教程并使其发挥作用?
【问题讨论】:
【参考方案1】:问题是我在 Google 控制台开发者网站上使用 Type : "Others"。当我切换回类型时:“Android”。它正在工作。
【讨论】:
以上是关于Xamarin.Auth:使用身份提供者对用户进行身份验证 [xamarin forms - Android]的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Auth 或 IdentityModel + IdentityServer4
Java.Lang.ClassNotFoundException WebAuthenticatorActivity
使用第 3 方 Oauth 提供程序对 API 用户进行身份验证 [关闭]
防止 Spring Security 通过下一个身份验证提供程序对具有 BadCredentialException 的用户进行身份验证