Insert data into google fit failing: UNKNOWN_AUTH_ERROR/NEEDS_OAUTH_PERMISSIONS

白昼怎懂夜的黑 提交于 2019-12-12 01:42:43

问题


I have the following code for inserting hydration data using History Api,

public void addHydrationData(int waterVolume){
    Calendar cal = Calendar.getInstance();
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.MINUTE, -1);
    long startTime = cal.getTimeInMillis();
    float volume = (float)waterVolume/1000f;
    S.L("V F: " + volume + "wv: " + waterVolume);
    final DataSet hydrationDataSet = createDataForRequest(DataType.TYPE_HYDRATION, DataSource.TYPE_RAW,
           volume, startTime, endTime, TimeUnit.MINUTES);

    Thread thread = new Thread(new Runnable() {
        public void run() {
            S.L("gac isconn: " + googleApiClient.isConnected());
            S.L("gac gcr: " + googleApiClient.getConnectionResult(Fitness.HISTORY_API));
            Status waterInsertStatus = Fitness.HistoryApi.insertData(googleApiClient, hydrationDataSet).await(1, TimeUnit.MINUTES);
            if(!waterInsertStatus.isSuccess()){
                S.L("There was a problem inserting the dataset.");
                S.L("WIS: " + waterInsertStatus.getStatusCode());
                if (waterInsertStatus.getStatusCode() == FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS ||
                        waterInsertStatus.getStatusCode() == FitnessStatusCodes.UNKNOWN_AUTH_ERROR) {
                    try {
                        S.L("Need oauth");
                        waterInsertStatus.startResolutionForResult(MainActivity.this, S.REQUEST_OAUTH);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                        S.L("SIE");
                    }
                } else{

                }
            } else {
                S.L("Successfully added data");
            }
        }
    });
    thread.start();
}

This is the output I am getting:

08-19 14:51:36.423 4594-4594/in.jiyofit.gfit D/CCC: V F: 0.25wv: 250
08-19 14:51:36.454 4594-5585/in.jiyofit.gfit D/CCC: gac isconn: true
08-19 14:51:36.454 4594-5585/in.jiyofit.gfit D/CCC: gac gcr: ConnectionResult{statusCode=SUCCESS, resolution=null, message=null}
08-19 14:51:36.928 4594-5585/in.jiyofit.gfit D/CCC: There was a problem inserting the dataset.
08-19 14:51:36.928 4594-5585/in.jiyofit.gfit D/CCC: WIS: 5005
08-19 14:51:36.929 4594-5585/in.jiyofit.gfit D/CCC: Need oauth

Earlier I was getting the status code as 5000, now I am getting 5005. Both have to be resolved in the following way as suggested by Google

Status code denotes that the request is missing desired OAuth permissions.

If an app does not have the required OAuth access for a specific API request, the request will fail with this status code. When this occurs, apps can use the pending intent inside the status object to request the necessary access before retrying the request.

Sample usage when access is missing for a request:

 PendingResult<Result> pendingResult = FitnessApi.readData(fitnessRequest);
 Result result = pendingResult.await(3L, TimeUnit.SECONDS);
 Status = result.getStatus();

 if (!status.isSuccess()) {
      if (status.getStatusCode() == FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS) {
          status.startResolutionForResult(
                  myActivity,
                  MY_ACTIVITYS_AUTH_REQUEST_CODE);
      }
 }

But nothing is happening when the startResolutionForResult is called.


回答1:


Need to add new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE) in requestScopes in GoogleSignInOptions.Builder. Datatype TYPE_HYDRATION belongs to the scope FITNESS_NUTRITION. The app also needs to be uninstalled and reinstalled to add the new scope to the account.



来源:https://stackoverflow.com/questions/39035991/insert-data-into-google-fit-failing-unknown-auth-error-needs-oauth-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!