【问题标题】:Having trouble connecting to Google Play Services; Getting Cancelled status code无法连接到 Google Play 服务;获取已取消状态代码
【发布时间】:2016-06-23 19:10:09
【问题描述】:

所以我正在尝试使用 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<>();
}

【问题讨论】:

  • 请正确地提出问题。至少复制粘贴代码。
  • 抱歉,添加了连接 GoogleApiClient 的代码

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


【解决方案1】:

新的更新需要谷歌登录 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 的服务器端实现。否则上面的代码将不起作用

【讨论】:

  • 但是我有另一个不需要 GoogleSignInOptions 的应用程序。这什么时候变成了要求?
  • 最近...大约一个月前...教程都没有更新
  • 所以它仍然无法正常工作,即使我添加了它。事实上,当我使用它时,我的测试程序甚至无法正常工作。它没有给我任何数据。然后,当我将其注释掉时,它会为我提供所需的数据。
  • 这里给出了取消状态的解决方案:github.com/googlesamples/android-fit/issues/15
  • 好吧,解决了我的问题,但它很棘手只有一个原因 - 我从来没有意识到我的 Oauth 密钥是用于我的 debug.keystore,而不是用于发布。所以对于下一位读者 - 确保您已经为发布变体生成了 Oauth,而不是为调试版本。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2022-01-07
    • 2017-03-06
    相关资源
    最近更新 更多