因此,我正在尝试使用 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/