3

因此,我正在尝试使用 Google Fit,但由于某种原因,我无法在我的应用中连接到 Google Play 服务。我已经完全设置了 OAuth,并在另一个应用程序中对其进行了测试,一切正常。具体看看 GoogleFitRepository,这是我连接到 API 的地方。

尝试连接时出现此错误:

W/AutoManageHelper: Unresolved error while connecting client. Stopping auto-manage.
I/MyApp: Google Play services connection failed. Cause: ConnectionResult{statusCode=CANCELED, resolution=null, message=null}

GitHub链接:https ://github.com/drb56/FitnessExplorer

任何帮助将不胜感激。我整天都被困在这上面!

编辑:这是相关课程中的一些代码

public GoogleFitRepository(Activity activity)
{
    mClient = new GoogleApiClient.Builder(activity.getApplicationContext())
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.SESSIONS_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this)
            .enableAutoManage((FragmentActivity)activity, 0, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.i("MyApp", "Google Play services connection failed. Cause: " +
                            result.toString());
                }})
            .build();

    activityList = new ArrayList<>();
    activityDataPointList = new ArrayList<>();
    calorieActivitiesToday = new ArrayList<>();
    dayActivities = new ArrayList<>();
}
4

2 回答 2

6

新的更新需要 google signin api 与 fit api 一起执行。

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestScopes(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE), new Scope(Scopes.FITNESS_LOCATION_READ))
        .build();

googleApiClient = new GoogleApiClient.Builder(activity)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addConnectionCallbacks(connectionCallbacks)
        .addOnConnectionFailedListener(failedListener)
        .addApi(Fitness.HISTORY_API)
        .addApi(Fitness.SESSIONS_API)
        .addApi(Fitness.RECORDING_API)
        .addApi(Fitness.SENSORS_API)
        .enableAutoManage(this, 0, this)
        .build();

您必须在console.developers.google.com. 请通过 Google Auth API 的服务器端实现。否则上面的代码将不起作用

于 2016-06-25T09:10:28.890 回答
0

在使用库中的 GoogleFit 时,我遇到了同样的问题。经过长时间的研究,我发现,我必须提出:

   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

进入应用程序的清单而不是库的清单。

于 2018-03-07T17:17:04.403 回答