我有以下用于使用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();
}
这是我得到的输出:
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
之前我的状态代码是5000,现在我得到了5005.两者都必须按照Google的建议以下列方式解决
状态代码表示请求缺少所需的OAuth权限。
如果某个应用对特定API请求没有所需的OAuth访问权限,则该请求将失败并显示此状态代码。发生这种情况时,应用程序可以使用状态对象中的待处理意图在重试请求之前请求必要的访问权限。
请求缺少访问时的示例用法:
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);
}
}
但调用startResolutionForResult
时没有任何事情发生。
答案 0 :(得分:1)
需要在new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE)
的{{1}}中添加requestScopes
。数据类型GoogleSignInOptions.Builder
属于范围FITNESS_NUTRITION。还需要卸载并重新安装该应用程序,以便将新范围添加到帐户中。