java - 无法连接到 Google Play 服务;获取取消状态代码

标签 java android google-play google-fit google-fit-sdk

因此,我正在尝试使用 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<>();
}

最佳答案

新更新需要 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 上启用 Auth API。请通过 Google Auth API 的服务器端实现。否则上面的代码将无法运行

关于java - 无法连接到 Google Play 服务;获取取消状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37999892/

相关文章:

android - 是否有可能在凸起的按钮中获得 MaterialPageRoute

android - 尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'

android - Google Play 商店错误 DF-BPA-30

android - 我的应用程序在 Google Play 上的状态为草稿审核中

java - SAX 解析器返回空字符串

java - STOMP Spring WebSocket 消息超出大小限制

android - 注册接收器的正确模式?

android - 版本与 Google Play 商店中的设备不兼容

java - 如何使用 Struts 嵌套标签库从 HashSet 字段属性中获取项目?

java - 当从 Python 调用 jar 时,如何最小化 JavaVM 的加载时间?