使用 Google Fit api 时 Google Oauth 2.0 RESULT_CANCELED [英] Google Oauth 2.0 RESULT_CANCELED while using Google Fit api

查看:20
本文介绍了使用 Google Fit api 时 Google Oauth 2.0 RESULT_CANCELED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 android 应用中使用 google fit api.我按照 .

I am trying to use google fit api in my android app. I followed .

在我的应用中,我在此处获得 RESULT_CANCELED:

In my app, I get RESULT_CANCELED here:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if( requestCode == REQUEST_OAUTH ) {
        authInProgress = false;
        if( resultCode == RESULT_OK ) {
            if( !mClient.isConnecting() && !mClient.isConnected() ) {
                mClient.connect();
            }
        } else if( resultCode == RESULT_CANCELED ) {/// HERE
            Toast.makeText(MainActivity.this,"RESULT_CANCELED",Toast.LENGTH_SHORT).show();
            Log.e("GoogleFit", "RESULT_CANCELED");
            Log.e("GoogleFit", data.getExtras().toString());
        }
    }else if(requestCode == CALL_END){
        if (resultCode == Activity.RESULT_OK){
            //pass
        }else{

        }
    } else {
        Log.e("GoogleFit", "requestCode NOT request_oauth");
    }
}

试图找出最近两天的问题.我在 android studio 中添加了正确的播放服务库:compile 'com.google.android.gms:play-services-fitness:8.4.0'

Trying to figure out the problem from last two days. I have added correct play services library in android studio as: compile 'com.google.android.gms:play-services-fitness:8.4.0'

构建客户端:

private void buildFitnessClient() {

    if (mClient == null && checkPermissions()) {
        Log.i(TAG, "Building Fitness Client");
        mClient = new GoogleApiClient.Builder(this)
                .addApi(Fitness.SENSORS_API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mClient.connect();
    }
}

回调:

/**
 * GOOGLE FIT METHODS
 */
@Override
public void onConnected(@Nullable Bundle bundle) {
    DataSourcesRequest dataSourceRequest = new DataSourcesRequest.Builder()
            .setDataTypes( DataType.TYPE_STEP_COUNT_CUMULATIVE )
            .setDataSourceTypes( DataSource.TYPE_RAW )
            .build();

    ResultCallback<DataSourcesResult> dataSourcesResultCallback = new ResultCallback<DataSourcesResult>() {
        @Override
        public void onResult(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(mClient, dataSourceRequest)
            .setResultCallback(dataSourcesResultCallback);
}
@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();
                HealthRecordFragment.mStepsWalking.setText(value.toString());
            }
        });
    }
}

在 Gradle 中:

In Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.xxxx.xxxx"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        release {
            storeFile file("C:\Users\xxxxx\AndroidStudioProjects\HBEAT2\app\hbeat_android")
            storePassword "password"
            keyAlias "hbeat_android"
            keyPassword "password"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services-fitness:8.4.0'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile "com.android.support:design:23.2.1"
    compile 'com.github.rahatarmanahmed:circularprogressview:2.4.0'
    compile 'de.timroes.android:EnhancedListView:0.3.0'
}

推荐答案

我刚刚遇到了这个问题.我花了几个小时才弄明白,所以我想指出那些犯同样错误的人.

I've encountered this problem just now. Took me a few hours to figure out, so i would like to point out for those who make the same mistake.

我的 Google API 客户端无法仅在生产环境中连接,在开发环境中一切正常.

My Google API client cannot connect only on Production, everything works fine on Development environment.

这是因为我的应用设置为使用应用签名".有了这个,如果你使用Production Keystore的SHA1(或者图片中上传证书的SHA1)创建了Oauth Client ID,就不会使用了,因为google会删除这个证书,改成App Signing"证书

This is because my app was set up to use "App Signing". With this, if you created the Oauth Client ID using the Production Keystore's SHA1 (or Upload Certificate's SHA1 in the picture), it will not be used, because google will remove this certificate and change to the "App Signing" certificate

所以我们需要做的是解决这个问题:简单地使用这个新的 SHA1 创建一个新的 OAuth 客户端 ID,如下所示:

So what we need to do to fix the problem is: Simply create a new OAuth Client ID with this new SHA1 like so:

这个问题很容易遇到,因为大多数教程都会告诉您找到您的生产 SHA1 并在 API 控制台中使用它.但除非您使用应用签名"那么它就不再起作用了.

The issue is quite easy to encounter because most of the tutorials will tell you to find your Production SHA1 and use it in the API console. But except for if you use "App signing" then it won't work anymore.

这篇关于使用 Google Fit api 时 Google Oauth 2.0 RESULT_CANCELED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆