多权限范围的googlefitapi集成



我正在将Google fit API集成到一个开源项目中,我正在努力让用户使用Google帐户凭据并通过用户同意流程登录。当我尝试在登录Uri上传递额外的作用域权限时,会出现此错误。我不确定我的URL编码是否有问题,因为我确信API需要一个范围URL数组。在Google适合的API集成中,是否可以将多个权限放在一个oauth流中?

第一个URL运行良好,但其他URL没有重定向而是出现错误。

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

你查看过谷歌的Android入门文档吗?以下是他们添加作用域的示例代码:

mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addConnectionCallbacks(this)
.build()

您可以尝试使用addApi()addScope()来获得要使用的Scope的权限。

根据本教程:Google Fit for Android:Sessions API,可以为Google Fit API集成添加多个权限范围。

这是他们的样本代码:

@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();
}

编辑:

对于web,我认为您可以使用通过OAuth 2.0 请求额外权限

auth2 = gapi.auth2.init({
client_id: 'CLIENT_ID.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin', /** Default value **/
scope: 'profile' });                /** Base scope **/

只要需要额外的作用域,就可以通过构建一个包含要添加的作用域的选项生成器来请求它们,然后调用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}));
    });

将此添加到node.js中,然后这里是Google FIT web 的范围列表

HTH

最新更新