3
我仍然习惯于Xamarin.Forms,我处于非常基础的阶段。我为我的问题阅读了许多文章,但最终无法解决它。所以...Xamarin.Forms Google API使用身份提供商对用户进行身份验证
目前我正在尝试在我的Xamarin.Forms应用程序中使用Droid和iOS(无WP)添加Google身份验证。 到目前为止,我从here以下指南。我正在使用Xamarin.Auth向Google进行身份验证。
这里是我的源代码的一部分。
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);
一切看起来正常的和调试我下面说的编译器熄灭方法没有任何错误,但后来我收到的例外,你可以看到here。这是“没有兼容代码运行”。
这里的一些详细信息,关于我的源代码:用于客户端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; }
- 来源在哪里添加
[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(); } }
的
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()); } }
- 源下面是我通过深=>Link 1去的主要条款, Link 2 an d Link 3,但仍无法解决问题。
我不确定错误来自哪里,或者我可以继续调试以解决问题。
在此先感谢
解决方案
- 改变目前Android编译到Android 7.0的Android里面项目属性。 Screenshot
- 请确保Android内部清单您的目标是SDK版本。 Screenshot
- 将所有“Xamarin.Android。*”nuget包更新到最低版本25.4.0.1。很可能他们目前是23岁。3.0。我发现依赖关系更新时出现问题,所以我会手动上传。我去手动下载每个软件包并将其移动到软件包文件夹中。然后我创建了我自己的软件包源代码,并为我的文件夹软件包提供路径,并使用它来安装已下载的NuGet软件包。 Screenshot
- 之后,我的问题解决了。
请不要将源代码作为图像加入。还没有链接的图像。应该将源代码作为搜索和辐射能力的文本加入。 –