android - 将体重值保存到 Google Fit 时为 "Application needs OAuth consent from the User"

标签 android google-fit

我正在使用以下代码创建 GoogleApiClient 实例:

    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(act)
        .addApi(Fitness.HISTORY_API)
        .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
        .addConnectionCallbacks(this)
        .enableAutoManage(act, 0, this)
        .build();

创建客户端后,手机会显示帐户选择对话框,然后是 OAuth 授权对话框。之后, Activity 通过以下方法获取结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.i(TAG, "Result received");
    authInProgress = false;
    gf.connect();
}

在日志中,我可以看到授权过程正在运行,但在尝试保存到 Google Fit 时,使用以下代码:

    Calendar calendar = Calendar.getInstance();
    Date now = new Date();
    calendar.setTime(now);
    long date = calendar.getTimeInMillis();

    DataSource dataSource = new DataSource.Builder().
            setAppPackageName(activity).
            setDataType(dataType).
            setType(DataSource.TYPE_RAW).
            build();
    DataSet dataSet = DataSet.create(dataSource);
    DataPoint dataPoint = dataSet.
            createDataPoint().
            setTimestamp(date, TimeUnit.MILLISECONDS);
    dataPoint.
            getValue(field).
            setFloat(value);
    dataSet.add(dataPoint);

    Fitness.HistoryApi.insertData(mClient, dataSet).setResultCallback(cb);

我看到以下错误:

Application needs OAuth consent from the User

我错过了什么? OAuth 凭据已在 Google 控制台中创建。

最佳答案

您应该在初始化 mClient 时添加更多范围: 示例:

mClient = new GoogleApiClient.Builder(act)
    .addApi(Fitness.HISTORY_API)
    .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
    .addScope(new Scope(Scopes.FITNESS_NUTRITION_READ))

更多取决于您想要获取数据的值(value)。

关于android - 将体重值保存到 Google Fit 时为 "Application needs OAuth consent from the User",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36580147/

相关文章:

android - 如何在android中使用GmailSender发送带附件的电子邮件

php - 通过 Google Fit REST API 提交体重信息/距离

android - 如何为以下代码实现onCompletion监听器

android - 当屏幕宽度较小时,如何在右对齐按钮旁边省略居中对齐的 TextView

android - 如何使用健身 api 获取距离?

Android:如何从可穿戴设备获取 Google Fit 数据?

Android Wear Watch Face - INVALID_ACCOUNT 尝试连接到 Google Services API 时

java - 将高度插入 Google Fit API 时出现字段超出范围错误

android - 如何从 ACTION_VIEW Intent 中排除特定应用程序?

java - android/rails 分段上传问题