gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:40 25 4
gpt4 key购买 nike

我正在使用以下代码创建 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com