Flutter健康包-访问被拒绝!-谷歌适合



我正在Flutter中开发一个带有健康包的应用程序,但我的应用程序不起作用。

我下载了google-services.json,已经用包名编辑了清单文件,并使用了以下权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /

我使用我在这个包的页面上找到的例子:

// create a HealthFactory for use in the app
HealthFactory health = HealthFactory();
// define the types to get
var types = [
HealthDataType.STEPS,
HealthDataType.WEIGHT,
HealthDataType.HEIGHT,
HealthDataType.BLOOD_GLUCOSE,
];
// requesting access to the data types before reading them
bool requested = await health.requestAuthorization(types);
var now = DateTime.now();
// fetch health data from the last 24 hours
List<HealthDataPoint> healthData = await health.getHealthDataFromTypes(
now.subtract(Duration(days: 1)), now, types);
// request permissions to write steps and blood glucose
types = [HealthDataType.STEPS, HealthDataType.BLOOD_GLUCOSE];
var permissions = [
HealthDataAccess.READ_WRITE,
HealthDataAccess.READ_WRITE
];  
await health.requestAuthorization(types, permissions: permissions);
// write steps and blood glucose 
bool success = await health.writeHealthData(10, HealthDataType.STEPS, now, now);
success = await health.writeHealthData(3.1, HealthDataType.BLOOD_GLUCOSE, now, now);
// get the number of steps for today
var midnight = DateTime(now.year, now.month, now.day);
int? steps = await health.getTotalStepsInInterval(midnight, now);

控制台日志如下所示:

D/FLUTTER_HEALTH(17489): Access Denied!
W/FLUTTER_HEALTH::ERROR(17489): There was an error adding the DataSet
W/FLUTTER_HEALTH::ERROR(17489): com.google.android.gms.common.api.ApiException: 4: The user must be signed in to make this API call.
W/FLUTTER_HEALTH::ERROR(17489):     at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@18.0.1:3).

请帮帮我…我已经试了很长时间了:(

错误表明您必须登录才能进行API调用。您必须生成OAuth客户端ID、句柄范围等,才能使用API。这是通过GCP完成的。此外,请确保将电子邮件添加为项目的测试用户。

遵循以下步骤

-使用密钥库(build.gradle(在应用程序中配置签名。查看guidlinehttps://docs.flutter.dev/deployment/android

-配置oauth客户端

  • 查看具有相同id客户端的google-services.json

然后运行!

最新更新