2017-08-14 7 views
3

私はまだXamarin.Formsに慣れていて、私は非常に基本的なレベルです。私は問題のために多くの記事を読んだが、最終的にそれを解決することはできなかった。だから...Xamarin.Forms Google API IDプロバイダを使用してユーザーを認証する

現在、DroidとiOS(WPなし)を使用するXamarin.Formsアプリケーション内に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; 
        } 
    
    
    • 私はここでUrlSchemeInterceptorActivity
     
    
        [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を経てメイン記事です追加のAndroid MainActivityのソースLink 2 d Link 3、しかしそれでも問題を解決できませんでした。

    エラーの原因がわからない場合や、問題を解決するためにデバッグを続行できますか。事前に

    おかげ

    ソリューション

    1. のAndroidプロジェクトのプロパティの内側のAndroid 7.0への変更アンドロイドコンパイラ。 Screenshot
    2. Androidマニフェスト内でターゲットがSDKバージョンであることを確認してください。 Screenshot
    3. すべての「Xamarin.Android。*」ナゲットパッケージを最小バージョン25.4.0.1にアップデートしてください。おそらく彼らは現在23歳です。3.0。私はそれを更新する依存関係の問題を発見したので、私は手動アップロードを行います。私は行って、それぞれのパッケージを手動でダウンロードしてパッケージフォルダに移動します。それから、私自身のパッケージソースを作成し、私のフォルダパッケージのパスを指定し、それを使ってすでにダウンロードしたNuGetパッケージをインストールしました。 Screenshot
    4. その後、私の問題は解決されました。
+1

ソースコードをイメージとして含めないでください。まだリンクされていない画像。ソースコードは、検索と放射能のためのテキストとして含める必要があります。 –

答えて

1

Android SDKをAPI 24以上にアップデートし、プロジェクトのコンパイルバージョンとして設定してください。また、参照されているアセンブリのカスタムタブとその依存関係をv25.x.xに更新します。それでおしまい。あなたはそれを働かせることができるはずです。

ビジェイ。

関連する問題