问题
I'm trying to build a step counter application, as a test i downloaded the android fit code from github and ran the basicsensorsAPI:
googlesamples/android-fit
In order to get stepcount instead of location I changed the data type to TYPE_STEP_COUNT_CUMULATIVE and TYPE_DERIVED, (the orignials are TYPE_LOCATION_SAMPLE and TYPE_RAW). But as soon as I do this the OAUTH stops working, and i'm not sure why this is creating an issue.
Here is the changed code:
private void findFitnessDataSources() {
// [START find_data_sources]
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_DERIVED)
.build())
.setResultCallback(new ResultCallback<DataSourcesResult>() {
@Override
public void onResult(DataSourcesResult dataSourcesResult) {
Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
for (DataSource dataSource : dataSourcesResult.getDataSources()) {
Log.i(TAG, "Data source found: " + dataSource.toString());
Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
//Let's register a listener to receive Activity data!
if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_CUMULATIVE)
&& mListener == null) {
Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_STEP_COUNT_CUMULATIVE);
}
}
}
});
// [END find_data_sources]
}
Here's the original code:
private void findFitnessDataSources() {
// [START find_data_sources]
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_LOCATION_SAMPLE)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_RAW)
.build())
.setResultCallback(new ResultCallback<DataSourcesResult>() {
@Override
public void onResult(DataSourcesResult dataSourcesResult) {
Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
for (DataSource dataSource : dataSourcesResult.getDataSources()) {
Log.i(TAG, "Data source found: " + dataSource.toString());
Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
//Let's register a listener to receive Activity data!
if (dataSource.getDataType().equals(DataType.TYPE_LOCATION_SAMPLE)
&& mListener == null) {
Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_LOCATION_SAMPLE);
}
}
}
});
// [END find_data_sources]
}
I get this output: Application needs oAuth consent from the User.
回答1:
I had also faced this problem.
You need to ask for a permission if you face this problem: just add this code inside your onResult()
method
if (dataSourcesResult.getStatus().getStatusCode()==5000)
{
try {
dataSourcesResult.getStatus().startResolutionForResult(SplashActivity.this,10);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
and in onActivityResult
, call again your method
if (requestCode==10 && resultCode==RESULT_OK)
{
findFitnessDataSources();
}
You can also check here for more info here. Please visit this url. It can explain you all the status code and error regarding google fit
回答2:
Its about scope. visit this google developer page
Data type for which you are adding data should be in scope. means if you want to add
DataType.TYPE_DISTANCE_CUMULATIVE
You need to add relevant scope. which is
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
and If you want to add
DataType.TYPE_STEP_COUNT_DELTA
than you need to add scope
https://www.googleapis.com/auth/fitness.activity.write
or "FITNESS_ACTIVITY_READ_WRITE"
回答3:
According to this Getting Started on Android:
Step 3: Get an OAuth 2.0 client ID
If you haven't already enabled the Fitness API and obtained an OAuth 2.0 client ID, follow the instructions in Get an OAuth 2.0 Client ID to do so now.
Also,
Authorization on Android
User consent is always required before your app can read or write fitness data. To obtain authorization:
- Register your Android app with a project in the Google Developers Console.
- Specify a scope of access when connecting to the fitness service.
In Google Fit, scopes are strings that determine what kinds of fitness data an app can access and the level of access to this data.
The authorization flow is the following:
- Your app requests a connection, with one or more scopes of access, to the fitness service.
- Google Fit prompts the user to grant your app the required permissions.
- If the user consents, your app can access fitness data of the types defined by the scope.
The specific permissions requested from the user depend on the scopes that your app specifies when connecting to the service.
Connect to the Google Fit Platform, and then check and request permissions if required by the Fit APIs that you use in your app:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Put application specific code here.
setContentView(R.layout.activity_main);
// This method sets up our custom logger, which will print all log messages to the device
// screen, as well as to adb logcat.
initializeLogging();
// When permissions are revoked the app is restarted so onCreate is sufficient to check for
// permissions core to the Activity's functionality.
if (!checkPermissions()) {
requestPermissions();
}
}
Create the Google API client and provide the required callback methods:
**
* Build a {@link GoogleApiClient} that will authenticate the user and allow the application
* to connect to Fitness APIs. The scopes included should match the scopes your app needs
* (see documentation for details). Authentication will occasionally fail intentionally,
* and in those cases, there will be a known resolution, which the OnConnectionFailedListener()
* can address. Examples of this include the user never having signed in before, or having
* multiple accounts on the device and needing to specify which account to use, etc.
*/
private void buildFitnessClient() {
if (mClient == null && checkPermissions()) {
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
findFitnessDataSources();
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i
== ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG,
"Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
Snackbar.make(
MainActivity.this.findViewById(R.id.main_activity_view),
"Exception while connecting to Google Play services: " +
result.getErrorMessage(),
Snackbar.LENGTH_INDEFINITE).show();
}
})
.build();
}
}
来源:https://stackoverflow.com/questions/36773519/google-fit-api-oauth-issue