Google健身用户登录授权访问权限永远不会通过

时间:2016-04-26 02:07:25

标签: android google-api fragment google-fit google-fit-sdk

我正在尝试将Google Fitness API集成到我的应用中。

当它要求用户登录以授予Google Fit时,它永远不会通过。 我总是在RESULT_CANCELED获得onActivityResult()

问题是什么?

以下是我的实施。

// Google Fit Related
public static final int REQUEST_OAUTH = 10101;
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;
private GoogleApiClient mGoogleApiClient;
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;


private void coonectGoogleFit() {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addApi(Fitness.SENSORS_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}


@Override
public void onConnected(Bundle bundle) {

    DataSourcesRequest dataSourcesRequest = new DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
            .setDataSourceTypes(DataSource.TYPE_RAW)
            .build();


    ResultCallback<DataSourcesResult> dataSourcesResultResultCallback = new ResultCallback<DataSourcesResult>() {
        @Override
        public void onResult(DataSourcesResult dataSourcesResult) {
            for(DataSource dataSource : dataSourcesResult.getDataSources()) {
                if(DataType.TYPE_STEP_COUNT_DELTA.equals(dataSource.getDataType())) {
                    registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_DELTA);
                }
            }
        }
    };


    Fitness.SensorsApi.findDataSources(mGoogleApiClient, dataSourcesRequest)
            .setResultCallback(dataSourcesResultResultCallback);

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

    if(!authInProgress) {
        try {
            authInProgress = true;
            Log.e("ConnectionResult", connectionResult+"");
            connectionResult.startResolutionForResult(getActivity(), REQUEST_OAUTH);
        } catch (IntentSender.SendIntentException e) {
            Log.e("IntentSender", e.getMessage());
        }
    } else {
        Log.e("GoogleFit", "authInProgress");
    }

}

@Override
public void onDataPoint(DataPoint dataPoint) {
    for(final Field field : dataPoint.getDataType().getFields()) {
        final Value value = dataPoint.getValue(field);
        new Runnable() {
            @Override
            public void run() {
                circleProgressBar.setProgressWithAnimation(value.asFloat(), 500);
                circleProgressBar.setText(value.asFloat()+"", 72, Color.parseColor("#25DDA5"));
            }
        };
    }
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_OAUTH) {
        authInProgress = false;
        Log.i("ResultCode", resultCode+"");
        Log.i("CANCELED", getActivity().RESULT_CANCELED+"");
        if( resultCode == getActivity().RESULT_OK ) {
            Log.e("GoogleFit", "RESULT_OK");
            if(!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        } else if(resultCode == getActivity().RESULT_CANCELED) {
            Log.e("GoogleFit", "RESULT_CANCELED");
        }
    } else {
        Log.e("GoogleFit", "requestCode Not request_oauth");
    }
}


private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {

    SensorRequest request = new SensorRequest.Builder()
            .setDataSource(dataSource)
            .setDataType(dataType)
            .setSamplingRate(1, TimeUnit.SECONDS)
            .build();

    Fitness.SensorsApi.add(mGoogleApiClient, request, this)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if(status.isSuccess()) {
                        Log.i("GoogleFit", "Sensor Api successfully Added");
                    }
                }
            });
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过Getting Started on Android (GoogleFit)

基于Google示例实施onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
// Receiving a result from the AccountPicker
if (resultCode == RESULT_OK) {
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
// With the account name acquired, go get the auth token
getUsername();
} else if (resultCode == RESULT_CANCELED) {
// The account picker dialog closed without selecting an account.
// Notify users that they must pick an account to proceed.
Toast.makeText(this, R.string.pick_account, Toast.LENGTH_SHORT).show();
}
} else if ((requestCode == REQUEST_CODE_RECOVER_FROM_AUTH_ERROR ||
requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR)
&& resultCode == RESULT_OK) {
// Receiving a result that follows a GoogleAuthException, try auth again
getUsername();
}
}

您的应用中可能没有OAuth /授权,您可以按照Get an OAuth 2.0 Client ID中的步骤验证您是否已授权您的应用使用Google健身。

我希望它有所帮助。