2016-02-26 77 views
4

我想要使用Google Fit History API,並且遇到問題,在使用ConnectionResult.StartResolutionForResult提示用戶使用Google帳戶後,我總是得到即使用戶通過對話框選擇帳戶也會取消。據我所知,我已按照此處發現的指南(https://developers.google.com/fit/android/get-api-key)致函。我在我的Developers控制檯中有一個項目。我已經在控制檯中啓用了Fitness API。我在開發機器上使用調試密鑰庫生成了一個客戶端ID。下面是從開發人員控制檯的一些截圖: Screenshot of API enabled in developer console Screenshot of OAuth Client ID in developer consoleGoogle API客戶端OAuth始終返回取消

我在Xamarin.Android編程和遵循這裏的例子。 (請注意,我有安裝包Xamarin.GooglePlayServices.Fitness): https://github.com/xamarin/monodroid-samples/tree/master/google-services/Fitness/BasicHistoryApi

這裏是代碼的關鍵領域:

mClient = new GoogleApiClient.Builder (this) 
     .AddApi (FitnessClass.HISTORY_API) 
     .AddScope (new Scope (Scopes.FitnessActivityReadWrite)) 
     .AddConnectionCallbacks (clientConnectionCallback) 
     .AddOnConnectionFailedListener (result => { 
      Log.Info (TAG, "Connection failed. Cause: " + result); 
      if (!result.HasResolution) { 
       // Show the localized error dialog 
       GooglePlayServicesUtil.GetErrorDialog (result.ErrorCode, this, 0).Show(); 
       return; 
      } 
      // The failure has a resolution. Resolve it. 
      // Called typically when the app is not yet authorized, and an 
      // authorization dialog is displayed to the user. 
      if (!authInProgress) { 
       try { 
        Log.Info (TAG, "Attempting to resolve failed connection"); 
        authInProgress = true; 
        result.StartResolutionForResult (this, REQUEST_OAUTH); 
       } catch (IntentSender.SendIntentException e) { 
        Log.Error (TAG, "Exception while starting resolution activity", e); 
       } 
      } 
     }).Build(); 

...

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) 
    { 
     if (requestCode == REQUEST_OAUTH) { 
      authInProgress = false; 
      if (resultCode == Result.Ok) { 
       // Make sure the app is not already connected or attempting to connect 
       if (!mClient.IsConnecting && !mClient.IsConnected) { 
        mClient.Connect(); 
       } 
      } 
     } 
    } 

的OnFailedConnectionListener是使用statusCode = SIGN_IN_REQUIRED調用,然後使我調用StartResolutionForResult並彈出用戶選擇其Google帳戶的對話框。只要顯示對話框,我在我的LogCat中出現以下錯誤。請注意,這是他們選擇帳戶之前。

02-26 15:56:36.459: E/MDM(17800): [63567] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null} 

一旦用戶選擇了帳戶,OnActivityResult被調用和resultCode爲總是「取消」,這是應該指示用戶駁回了對話,但是這肯定不是這裏發生了什麼。任何幫助?它在開發人員控制檯中看起來有點不對勁,但經過100次指導並獲得了相同的結果後,我開始瘋狂起來。

+0

您是否將證書添加到了android密鑰? http://stackoverflow.com/questions/30650231/couldnt-connect-to-google-api-client second anser – Gusman

回答

3

所以我的問題是,我使用了錯誤的debug.keystore。我的Mac同時安裝了Android Studio和Xamarin Studio。我錯誤地認爲Xamarin正在使用「〜/ .android/debug.keystore」,但事實證明,他們把它們放在「〜/ .local/share/Xamarin/Mono for Android/debug.keystore」改爲使用SHA1從這個關鍵解決了我的問題。對於我在Xamarin密鑰上的信息: https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/MD5_SHA1/#OSX

相關問題