2016-04-21 57 views
0

我試圖建立一個計步器程序,作爲測試我下載從GitHub Android的版型碼,並運行basicsensorsAPI:谷歌配合API OAUTH問題

googlesamples/android-fit

爲了得到STEPCOUNT代替的位置,我將數據類型更改爲TYPE_STEP_COUNT_CUMULATIVE和TYPE_DERIVED,(該標誌爲TYPE_LOCATION_SAMPLE和TYPE_RAW)。但只要我這樣做,OAuth就停止工作,我不知道爲什麼這會造成問題。

這裏是改變的代碼:

private void findFitnessDataSources() { 
    // [START find_data_sources] 
    // Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission. 
    Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder() 
      // At least one datatype must be specified. 
      .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE) 
        // Can specify whether data type is raw or derived. 
      .setDataSourceTypes(DataSource.TYPE_DERIVED) 
      .build()) 
      .setResultCallback(new ResultCallback<DataSourcesResult>() { 
       @Override 
       public void onResult(DataSourcesResult dataSourcesResult) { 
        Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString()); 
        for (DataSource dataSource : dataSourcesResult.getDataSources()) { 
         Log.i(TAG, "Data source found: " + dataSource.toString()); 
         Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName()); 

         //Let's register a listener to receive Activity data! 
         if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_CUMULATIVE) 
           && mListener == null) { 
          Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering."); 
          registerFitnessDataListener(dataSource, 
            DataType.TYPE_STEP_COUNT_CUMULATIVE); 
         } 
        } 
       } 
      }); 
    // [END find_data_sources] 
} 

這裏是原代碼:

private void findFitnessDataSources() { 
    // [START find_data_sources] 
    // Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission. 
    Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder() 
      // At least one datatype must be specified. 
      .setDataTypes(DataType.TYPE_LOCATION_SAMPLE) 
      // Can specify whether data type is raw or derived. 
      .setDataSourceTypes(DataSource.TYPE_RAW) 
      .build()) 
      .setResultCallback(new ResultCallback<DataSourcesResult>() { 
       @Override 
       public void onResult(DataSourcesResult dataSourcesResult) { 
        Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString()); 
        for (DataSource dataSource : dataSourcesResult.getDataSources()) { 
         Log.i(TAG, "Data source found: " + dataSource.toString()); 
         Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName()); 

         //Let's register a listener to receive Activity data! 
         if (dataSource.getDataType().equals(DataType.TYPE_LOCATION_SAMPLE) 
           && mListener == null) { 
          Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering."); 
          registerFitnessDataListener(dataSource, 
            DataType.TYPE_LOCATION_SAMPLE); 
         } 
        } 
       } 
      }); 
    // [END find_data_sources] 
} 

我得到這樣的輸出:應用程序從用戶需要在OAuth同意。

回答

0

根據這一Getting Started on Android

第3步:獲取OAuth 2.0客戶端ID

如果您尚未啓用的健身API並獲得一個OAuth 2.0客戶端ID,請在說明立即獲取OAuth 2.0客戶端ID。

此外,

Authorization on Android

用戶同意前,你的應用程序可以讀取或寫入數據的健身始終需要。獲取授權:

  • 在Google Developers Console中通過項目註冊您的Android應用。
  • 指定連接到健身服務時的訪問範圍。

在Google Fit中,作用域是確定應用程序可以訪問哪些健身數據以及訪問此數據的級別的字符串。

授權流程如下:

  • 您的應用程序請求連接時,訪問的一個或多個範圍,健身服務。
  • Google Fit會提示用戶爲您的應用授予所需的權限。
  • 如果用戶同意,您的應用可以訪問由範圍定義的類型的健身數據。

從用戶請求的特定權限取決於您的應用在連接到服務時指定的範圍。

連接到谷歌飛度平臺,然後檢查,並請求權限,如果需要的話通過你在你的應用程序中使用的適合的API:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Put application specific code here. 

    setContentView(R.layout.activity_main); 
    // This method sets up our custom logger, which will print all log messages to the device 
    // screen, as well as to adb logcat. 
    initializeLogging(); 

    // When permissions are revoked the app is restarted so onCreate is sufficient to check for 
    // permissions core to the Activity's functionality. 
    if (!checkPermissions()) { 
     requestPermissions(); 
    } 
} 

創建谷歌API客戶端,並提供必要的回調方法:

** 
* Build a {@link GoogleApiClient} that will authenticate the user and allow the application 
* to connect to Fitness APIs. The scopes included should match the scopes your app needs 
* (see documentation for details). Authentication will occasionally fail intentionally, 
* and in those cases, there will be a known resolution, which the OnConnectionFailedListener() 
* can address. Examples of this include the user never having signed in before, or having 
* multiple accounts on the device and needing to specify which account to use, etc. 
*/ 
private void buildFitnessClient() { 
    if (mClient == null && checkPermissions()) { 
     mClient = new GoogleApiClient.Builder(this) 
       .addApi(Fitness.SENSORS_API) 
       .addScope(new Scope(Scopes.FITNESS_LOCATION_READ)) 
       .addConnectionCallbacks(
         new GoogleApiClient.ConnectionCallbacks() { 
          @Override 
          public void onConnected(Bundle bundle) { 
           Log.i(TAG, "Connected!!!"); 
           // Now you can make calls to the Fitness APIs. 
           findFitnessDataSources(); 
          } 

          @Override 
          public void onConnectionSuspended(int i) { 
           // If your connection to the sensor gets lost at some point, 
           // you'll be able to determine the reason and react to it here. 
           if (i == ConnectionCallbacks.CAUSE_NETWORK_LOST) { 
            Log.i(TAG, "Connection lost. Cause: Network Lost."); 
           } else if (i 
             == ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { 
            Log.i(TAG, 
              "Connection lost. Reason: Service Disconnected"); 
           } 
          } 
         } 
       ) 
       .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() { 
        @Override 
        public void onConnectionFailed(ConnectionResult result) { 
         Log.i(TAG, "Google Play services connection failed. Cause: " + 
           result.toString()); 
         Snackbar.make(
           MainActivity.this.findViewById(R.id.main_activity_view), 
           "Exception while connecting to Google Play services: " + 
             result.getErrorMessage(), 
           Snackbar.LENGTH_INDEFINITE).show(); 
        } 
       }) 
       .build(); 
    } 
} 
0

我也遇到過這個問題。
你需要,如果你面對這個問題問的權限:只需添加此代碼的onResult()方法裏面onActivityResult

if (dataSourcesResult.getStatus().getStatusCode()==5000) 
{ 
    try { 
     dataSourcesResult.getStatus().startResolutionForResult(SplashActivity.this,10); 
    } catch (IntentSender.SendIntentException e) { 
     e.printStackTrace(); 
    } 
} 

,並再次打電話給你的方法

if (requestCode==10 && resultCode==RESULT_OK) 
{ 
    findFitnessDataSources(); 
} 

您也可以點擊這裏欲瞭解更多信息,請致電here。 請訪問此URL。它可以解釋你所有的狀態代碼和谷歌適合的錯誤

0

它關於範圍。請訪問此google developer page

要添加數據的數據類型應在範圍內。意味着您要添加

DataType.TYPE_DISTANCE_CUMULATIVE 

您需要添加相關範圍。這是

.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE)) 

,如果你想添加

DataType.TYPE_STEP_COUNT_DELTA 

比你需要添加範圍

https://www.googleapis.com/auth/fitness.activity.write

或 「FITNESS_ACTIVITY_READ_WRITE」