I am integrating Google fit API in an open source project am working on where I allow the user to login with the Google account credentials and through the user consent process. I have this error when I try to pass in additional scope permissions on the sign in Uri. I am not sure if it's a problem with my URL encoding because I am sure the API expects an array of scope urls. Is it possible to put multiple permissions in one oauth flow in Google fit API integration?
The first URL is working fine but the others get an error instead of redirecting.
https://accounts.google.com/ServiceLogin?passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?access_type%3Doffline%26as%3D43045f60390ad399%26approval_prompt%3Dforce%26scope%3Dhttps://www.googleapis.com/auth/fitness.activity.read%26response_type%3Dcode%26redirect_uri%3Dhttps://developers.google.com/oauthplayground%26client_id%3D1086862838918-d6epsnkqrid4tu786geh3nfugpga2ii5.apps.googleusercontent.com%26from_login%3D1&oauth=1&sarp=1&scc=1
https://accounts.google.com/ServiceLogin?passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?access_type%3Doffline%26as%3D43045f60390ad399%26approval_prompt%3Dforce%26scope%3D%5B%22https://www.googleapis.com/auth/fitness.activity.read%22%2%22https://www.googleapis.com/auth/fitness.activity.write%26response_type%3Dcode%22%5D%26redirect_uri%3Dhttps://developers.google.com/oauthplayground%26client_id%3D1086862838918-d6epsnkqrid4tu786geh3nfugpga2ii5.apps.googleusercontent.com%26from_login%3D1&oauth=1&sarp=1&scc=1
Have you check the Getting Started on Android documentation of Google? Here is their sample code for adding scope:
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addConnectionCallbacks(this)
.build()
You can try using addApi()
and addScope()
to get permission of Scope to be used.
From this tutorial:Google Fit for Android: Sessions API, it is possible to add multiple permission scope for Google Fit API integration.
Here is their sample code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SESSIONS_API)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this)
.enableAutoManage(this, 0, this)
.build();
}
For web I think you can use Requesting additional permissions via OAuth 2.0
auth2 = gapi.auth2.init({
client_id: 'CLIENT_ID.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin', /** Default value **/
scope: 'profile' }); /** Base scope **/
Wherever additional scopes are needed, request them by constructing an options builder with the scopes you want to add and then calling user.grant({scope: [OPTIONS BUILDER]}).then(successFunction, failFunction);
:
var options = new gapi.auth2.SigninOptionsBuilder(
{'scope': 'email https://www.googleapis.com/auth/drive'});
googleUser = auth2.currentUser.get();
googleUser.grant(options).then(
function(success){
console.log(JSON.stringify({message: "success", value: success}));
},
function(fail){
alert(JSON.stringify({message: "fail", value: fail}));
});
Add this to you node.js then here is the list of scope for Google FIT web
HTH