无法连接到google api,显示器上的结果是:
E / GoogleFit:RESULT_CANCELED (此消息仅在选择Google帐户后显示)
显然,我在我的应用上拥有互联网权限。
在Google开发者控制台上,该软件包的名称与我的项目相同。以下是代码:
public class MainActivity extends AppCompatActivity implements OnDataPointListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final int REQUEST_OAUTH = 1;
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;
private GoogleApiClient mApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null){
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}
mApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.build(); {
}
}
@Override
protected void onStart() {
super.onStart();
mApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
DataSourcesRequest dataSourcesRequest = new DataSourcesRequest.Builder()
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
.setDataSourceTypes(DataSource.TYPE_RAW)
.build();
ResultCallback<DataSourcesResult> dataSourcesResultResultCallback = new ResultCallback<DataSourcesResult>() {
@Override
public void onResult(@NonNull DataSourcesResult dataSourcesResult) {
for(DataSource dataSource : dataSourcesResult.getDataSources()){
if(DataType.TYPE_STEP_COUNT_CUMULATIVE.equals(dataSource.getDataType())){
registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_CUMULATIVE);
}
}
}
};
Fitness.SensorsApi.findDataSources(mApiClient, dataSourcesRequest).setResultCallback(dataSourcesResultResultCallback);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if(!authInProgress){
try{
authInProgress = true;
connectionResult.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
}catch (IntentSender.SendIntentException e){
}
}else{
Log.e("GoogleFit", "AuthInProgress");
}
}
@Override
public void onDataPoint(DataPoint dataPoint) {
for(final Field field: dataPoint.getDataType().getFields()){
final Value value = dataPoint.getValue(field);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Field"+field.getName()+"Value:"+ value,Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_OAUTH){
authInProgress = false;
if(resultCode == RESULT_OK){
if(!mApiClient.isConnecting() && !mApiClient.isConnected()){
mApiClient.connect();
}
}else if(resultCode == RESULT_CANCELED){
Log.e("GoogleFit", "RESULT_CANCELED");
}
}else {
Log.e("GoogleFit", "requestCode NOT request_oauth");
}
super.onActivityResult(requestCode, resultCode, data);
}
private void registerFitnessDataListener(DataSource dataSource, DataType dataType){
SensorRequest request = new SensorRequest.Builder()
.setDataSource(dataSource)
.setDataType(dataType)
.setSamplingRate(3, TimeUnit.SECONDS)
.build();
Fitness.SensorsApi.add(mApiClient, request, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if(status.isSuccess()){
Log.e("GoogleFit", "SensorApi succesfully added");
}
}
});
}
@Override
protected void onStop() {
super.onStop();
Fitness.SensorsApi.remove(mApiClient, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if(status.isSuccess()){
mApiClient.disconnect();
}
}
});
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putBoolean(AUTH_PENDING, authInProgress);
}
}
答案 0 :(得分:1)
修复问题:
嗯,我已经做了一些调查,我找到了解决方案。
代码运行良好,问题出在OAuthClient上。
避免此问题的步骤:
第1步:
转到Google开发控制台并启用API(在我的情况下为Fitness API),这一步非常重要,为了确保您创建OAuth凭据,只需使用给定的命令并写入SHA1
第2步:
转到Firebase控制台并按照步骤
第3步:
下载JSON并放入您的app文件夹(在您的项目中)
第4步:
在清单中创建这样的元数据:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="-key of your JSON-" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
希望对其他人有帮助!这个解决方案对我有用!
答案 1 :(得分:0)
确保使用正确的OAuth 2.0 Client ID代码。如果您使用错误,可能会导致错误。另外,请确保您已根据Google登录的最新documentation更新了身份验证流程。
您可以参考这些SO主题:Google Oauth 2.0 RESULT_CANCELED while using Google Fit api和GoogleApiClient not connecting while using google fit。