Reputation: 66575
For users who allowed the service to access their data I would like to implement a function to retrieve and handle these data.
When the user performs authentication for the first time, the following screen appears:
In google developers console I have created a service account, checked "Enable Google Apps Domain-Wide Delegation", and downloaded json file and included it in the following code:
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var fit = google.fitness('v1');
var key = require('../../config/meelio_google_service_account.json');
/*
This file contains following properties:
{
"type": "service_account",
"project_id": "meelio-dev",
"private_key_id": "***",
"private_key": "-----BEGIN PRIVATE KEY-----
***",
"client_email": "[email protected]",
"client_id": "1022202xxx41842",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/mfit-205%40meelio-dev.iam.gserviceaccount.com"
}
*/
var oauth2Client = new OAuth2(key.client_id, key.private_key, 'http://localhost:3000/api/auth/google/oauth2callback');
var scopes = ['https://www.googleapis.com/auth/fitness.activity.read',
'https://www.googleapis.com/auth/fitness.activity.write',
'https://www.googleapis.com/auth/fitness.body.read',
'https://www.googleapis.com/auth/fitness.body.write',
'https://www.googleapis.com/auth/fitness.location.read',
'https://www.googleapis.com/auth/fitness.location.write'];
module.exports.listUserDataSourcesTest = function(req, res){
var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, scopes, null);
jwtClient.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
}
oauth2Client.setCredentials({
access_token: tokens.access_token
});
// Make an authorized request to list Drive files.
fit.users.dataSources.list({auth: oauth2Client, userId: '104169835623446790746'}, function(err, resp){
console.log(resp);
});
});
}
When google fitness function is executed (fit.user.dataSources.list()) it returns an error:
"Unauthorized access"
As I am not sure whether the error is caused by inappropriate permission (or other) settings or wrong code, I would be very thankful if anyone could help out to solve this issue by suggestin a correct way to retrieve user's data and/or fix settings in google developers console.
Thanks!
Upvotes: 1
Views: 656
Reputation: 66575
According to documentation it is not possible to use other user's id:
Retrieve a dataset for the person identified. Use me to indicate the authenticated user. Only me is supported at this time.
Having available (e.g. stored in database) users token and access_token, it is possible to retrieve the data using the following function:
function listUserDataSources(access_token, refresh_token, res){
oauth2Client.setCredentials({
access_token: tokens.token,
refresh_token: tokens.refreshToken
});
fit.users.dataSources.list({auth: oauth2Client, userId: 'me'}, function(err, resp){
res.json([resp]);
});
}
Upvotes: 1