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
- 之後,我的問題解決了。
請不要將源代碼作爲圖像加入。還沒有鏈接的圖像。應該將源代碼作爲搜索和輻射能力的文本加入。 –