【问题标题】:Xamarin.Forms Google API Authenticating Users with an Identity ProviderXamarin.Forms Google API 使用身份提供者对用户进行身份验证
【发布时间】:2018-01-22 20:40:54
【问题描述】:

我仍然习惯于 Xamarin.Forms,而且我处于非常基础的水平。我为我的问题浏览了很多文章,但最终无法解决。所以……

目前我正在尝试在我的 Xamarin.Forms 应用程序中添加 Google 身份验证,该应用程序使用 Droid 和 iOS(无 WP)。 到目前为止,我正在遵循here 的指南。我正在使用 Xamarin.Auth 向 Google 进行身份验证。

这是我源代码的一部分。

私有异步无效 GoogleSheetsButton_Tapped() { 字符串 clientId = null; 字符串重定向Uri = null; if (Device.RuntimePlatform == Device.iOS) { clientId = 常量.iOSClientId; redirectUri = 常量.iOSRedirectUrl; } 否则如果(Device.RuntimePlatform == Device.Android) { clientId = 常量.AndroidClientId; redirectUri = 常量.AndroidRedirectUrl; } var 验证器 = 新 OAuth2Authenticator( 客户标识, 空值, 常量.范围, 新的 Uri(Constants.AuthorizeUrl), 新的 Uri(redirectUri), 新的 Uri(Constants.AccessTokenUrl), 空值, 真的); 身份验证器.Completed += OnAuthCompleted; 验证器.Error += OnAuthError; AuthenticationState.Authenticator = 身份验证器; var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter(); 演示者.登录(身份验证者); }

问题是在我的方法完成后出现的。所以在我的最后一行之后:

演示者.登录(身份验证者);

一切看起来都很好,调试我正在跟踪编译器没有任何错误的方法,但是我收到了异常,你可以看到here。它是“没有兼容的代码在运行”。

这里有一些关于我的源代码的更多信息:

  • 用于客户端 ID 和 URL 的“常量”类的来源
公共静态类常量 { 公共静态字符串 AppName = "...."; // OAuth // 对于谷歌登录,在 https://console.developers.google.com/ 进行配置 公共静态字符串 iOSClientId = "6.....apps.googleusercontent.com"; 公共静态字符串 AndroidClientId = "6.....apps.googleusercontent.com"; // 这些值不需要改变 公共静态字符串 Scope = "https://www.googleapis.com/auth/userinfo.email"; 公共静态字符串 AuthorizeUrl = "https://accounts.google.com/o/oauth2/auth"; 公共静态字符串 AccessTokenUrl = "https://www.googleapis.com/oauth2/v4/token"; 公共静态字符串 UserInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo"; // 将这些设置为反向的 iOS/Android 客户端 ID,并附加 :/oauth2redirect 公共静态字符串 iOSRedirectUrl = "com.googleusercontent.apps.6......h:/oauth2redirect"; 公共静态字符串 AndroidRedirectUrl = "com.googleusercontent.apps.6......l:/oauth2redirect"; }
  • 身份验证完成/错误的实现方法的来源,实际上由于我的错误我仍然无法命中
异步无效 OnAuthCompleted(对象发送者,AuthenticatorCompletedEventArgs e) { var authenticationator = 作为 OAuth2Authenticator 的发件人; 如果(身份验证器!= null) { 身份验证器.Completed -= OnAuthCompleted; 身份验证器.Error -= OnAuthError; } GoogleLoginUser 用户 = null; if (e.IsAuthenticated) { var request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account); var response = await request.GetResponseAsync(); 如果(响应!= null) { 字符串 userJson = 等待响应。GetResponseTextAsync(); 用户 = JsonConvert.DeserializeObject(userJson); } 如果(_account!= null) { _store.Delete(_account, Constants.AppName); } await _store.SaveAsync(_account = e.Account, Constants.AppName); await DisplayAlert("电子邮件地址", user.Email, "OK"); } } void OnAuthError(对象发送者,AuthenticatorErrorEventArgs e) { var authenticationator = 作为 OAuth2Authenticator 的发件人; 如果(身份验证器!= null) { 身份验证器.Completed -= OnAuthCompleted; 身份验证器.Error -= OnAuthError; } var message = e.Message; }
  • 我添加的 Android MainActivity 的来源
公共类 MainActivity : FormsAppCompatActivity { 受保护的覆盖无效 OnCreate(捆绑包) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = 资源.Layout.Toolbar; base.OnCreate(捆绑); Forms.Init(this, bundle); global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, bundle); MobileBarcodeScanner.Initialize(应用程序); 加载应用程序(新应用程序()); } }
  • UrlSchemeInterceptorActivity 的来源
[Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true, LaunchMode = LaunchMode.SingleTop)] [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDe​​fault, Intent.CategoryBrowsable }, DataSchemes = new[] { "com.googleusercontent.apps.6......l" } , DataPath = "/oauth2redirect")] 公共类 CustomUrlSchemeInterceptorActivity : 活动 { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); var uri = new Uri(Intent.Data.ToString()); AuthenticationState.Authenticator.OnPageLoading(uri); 结束(); } }

这是我深入阅读的主要文章 => Link 1Link 2Link 3,但仍然无法解决问题。

我不确定错误来自哪里,或者我可以继续调试它以解决问题。

提前致谢

解决方案

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

【问题讨论】:

  • 请不要将源代码作为图像包含在内。更不用说链接图像了。源代码应作为文本包含在搜索和 radability 中。

标签: c# .net xamarin xamarin.forms google-authentication


【解决方案1】:

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

维杰。

【讨论】:

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