所以我正在尝试使用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<>();
}
答案 0 :(得分: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
上启用Auth API。请仔细阅读Google Auth API的服务器端实现。否则上述代码将无效
答案 1 :(得分:0)
从库中使用GoogleFit时遇到了同样的问题。经过长时间的研究,我发现,我必须把:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
进入应用程序清单而不进入库的清单。