Android + Google 适合数据上传:错误 5002 - DataType 的名称与包名称不匹配

Android + Google Fit Data Upload : error 5002 - DataType's name does not match package name

在应用程序中,我们将用户 activity 数据上传到 Google Fit,如下所示:

   Fitness.getSessionsClient(context, GoogleSignIn.getLastSignedInAccount(context))
                            .insertSession(((SessionInsertRequest) object))
                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    // At this point, the session has been inserted and can be read.
                                    if (BuildConfig.DEBUG) {
                                        Log.i(TAG, "Session insert was successful!");
                                    }
                                     //more success handling
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {

                                    if (BuildConfig.DEBUG) {
                                        Log.w(TAG, "There was a problem inserting the session: " + e.toString()+ "\n"+ e.getLocalizedMessage());
                                    }
                                    //more error handling
                                    }
                                }
                            });

直到 2019 年 9 月 4 日左右,它一直运行良好。

然后 Google 似乎更改了某些内容,导致在创建 SessionInsertRequest

时出现以下警告

App com.foo.bar does not have access to data types in request

在 onFailureListener(...) 中,我们收到以下消息:

There was a problem inserting the session: com.google.android.gms.common.api.ApiException: 5002: DataType's name does not match package name.

数据类型未更改。当要求用户授予上传权限时,它们会被请求:

   private FitnessOptions getFitnessSignInOptions(OAuthType type) {

    switch (type) {

        case Activity:
            return FitnessOptions.builder()
                    .addDataType(DataType.TYPE_LOCATION_SAMPLE, FitnessOptions.ACCESS_WRITE)
                    .addDataType(DataType.TYPE_CALORIES_EXPENDED, FitnessOptions.ACCESS_WRITE)
                    .build();
    }
}

并在 DataSource 中创建,例如:

 DataSource  locationDataSource = new DataSource.Builder()
            .setAppPackageName(packageName)
            .setDataType(DataType.TYPE_LOCATION_SAMPLE)
            .setName(uniqueIdentifier + "-locations")
            .setType(DataSource.TYPE_RAW)
            .build();

Google sample 显示的相似。

如果我们省略设置数据源的名称或使用 setName(packageName) 都没有关系。同样使用 setStreamName(packageName) 也不能解决问题。 还有其他人有这个或类似的问题吗?

谢谢

罗伯特

我认为您可能提供了无效的 google-services.json 文件,或者至少提供了一个与您获得用户许可使用 [=16] 时使用的文件不兼容的文件=] 适合。 我遇到了同样的问题:当我在从 Google Play 下载的应用程序之上上传我的应用程序的新版本时,出现了这个错误。如果我从头开始安装新版本的应用程序,则不会发生这种情况。 在 Firebase 上重新生成 google-services.json 文件并将其提供给新版本的应用程序解决了这个问题。现在我可以在现有应用程序之上上传该应用程序,并且不再出现此错误。 但是请注意,在我没有启用 Firebase 之前,所以 Google 可能不允许再使用不是通过 Firebase

生成的 google-services.json

我想知道这个问题是否与 GoogleSignIn.getLastSignedInAccount 的使用有关。显然在新版本的库中,这是不受欢迎的。使用 GoogleSignIn.getAccountForExtension 应该可以解决问题。它适用于我的问题 with locations