Xamarin.Forms Google API使用身份提供程序对用户进行身份validation

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

目前我正在尝试在我的Xamarin.Forms应用程序中添加Google身份validation,该应用程序使用Droid和iOS(无WP)。 到目前为止,我从这里跟随指南。 我正在使用Xamarin.Auth对Google进行身份validation。

这是我的源代码的一部分。


     private async void GoogleSheetsButton_Tapped()
     {
         string clientId = null;
         string redirectUri = null;

         if(Device.RuntimePlatform == Device.iOS)
         {
             clientId = Constants.iOSClientId;
             redirectUri = Constants.iOSRedirectUrl;
         }
        否则if(Device.RuntimePlatform == Device.Android)
         {
             clientId = Constants.AndroidClientId;
             redirectUri = Constants.AndroidRedirectUrl;
         }

         var authenticator = new OAuth2Authenticator(
            的clientId,
            空值,
             Constants.Scope,
            新的Uri(Constants.AuthorizeUrl),
            新的Uri(redirectUri),
            新的Uri(Constants.AccessTokenUrl),
            空值,
            真正);

         authenticator.Completed + = OnAuthCompleted;
         authenticator.Error + = OnAuthError;

         AuthenticationState.Authenticator = authenticator;

         var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
         presenter.Login(认证器);
     }

问题出在我的方法完成它的工作之后。 所以在我的最后一行之后:


     presenter.Login(认证器);

一切看起来都没问题和调试我遵循编译器没有任何错误的方法,但后来我收到exception,你可以在这里看到。 这是“ 没有兼容的代码运行 ”。

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

  • 用于客户端ID和URL的“常量”类的来源

     public static class Constants
     {
         public static string AppName =“....”;

         // OAuth
         //对于Google登录,请访问https://console.developers.google.com/
         public static string iOSClientId =“6 ..... apps.googleusercontent.com”;
         public static string AndroidClientId =“6 ..... apps.googleusercontent.com”;

         //这些值不需要更改
         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”;

         //将这些设置为反向iOS / Android客户端ID,并附加:/ oauth2redirect
         public static string iOSRedirectUrl =“com.googleusercontent.apps.6 ...... h:/ oauth2redirect”;
         public static string AndroidRedirectUrl =“com.googleusercontent.apps.6 ...... l:/ oauth2redirect”;
     }

  • validation完成/错误的实现方法的来源,实际上我仍然因为我的错误而无法点击
 

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

            等待_store.SaveAsync(_account = e.Account,Constants.AppName);
            等待DisplayAlert(“电子邮件地址”,user.Email,“确定”);
         }
     }

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

         var message = e.Message;
     }

  • 我添加的Android MainActivity的来源

    公共类MainActivity:FormsAppCompatActivity
     {
         protected override void OnCreate(Bundle bundle)
         {
             TabLayoutResource = Resource.Layout.Tabbar;
             ToolbarResource = Resource.Layout.Toolbar;

             base.OnCreate(束);

             Forms.Init(this,bundle);
             global :: Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this,bundle);

             MobileBarcodeScanner.Initialize(应用程序);

             LoadApplication(new App());
         }
     }

  • 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:Activity {protected override void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState) ;  var uri = new Uri(Intent.Data.ToString());  AuthenticationState.Authenticator.OnPageLoading(URI); 完();  }} 

以下是我经历过的主要文章=> 链接1 , 链接2和链接3 ,但仍然无法解决问题。

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

提前致谢

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

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

维杰。