首页 文章

是否可以通过JS客户端库为Drive使用授权时正确选择任何可用的Google帐户?

提问于
浏览
21

我有一个现有的支持Google Drive的应用程序正在使用Google Java客户端库和服务器流程验证 .

如果您没有登录该应用程序并导航到该网址并且您已在该浏览器上登录了多个Google帐户(只有一个个人Google帐户,则其他任何一个都必须是Google商家帐户)OAuth回调优惠选择要使用的Google帐户的选项 .

但是,在测试使用JavaScript客户端库的开关时,我无法使用gapi.auth.authorize激活多个帐户选择屏幕 . 是否可以使用JS库处理多个帐户?

更新:我尝试使用 immediate 参数 false . 只要我不在弹出窗口中更改帐户,我就可以登录 . 如果我更改帐户,我会:

https://accounts.google.com/o/oauth2/auth?client_id=433863057149.apps.googleusercontent.com&scope=https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/drive.install+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&immediate=false&redirect_uri=postmessage&origin=https://drivedrawio.appspot.com&proxy=oauth2relay593063763&response_type=token&state=701344514&authuser=1

在一个新标签中没有任何反应 . 我made a video to demonstrate .

更新2:This bug针对JS客户端库,需要双重选择多个帐户已被接受 .

4 回答

  • 3

    由于以下参数,您没有获得多用户选择屏幕: authuser=0 这会自动选择您登录的第一个帐户( authuser=1 将选择第二个等...) .

    目前无法使用客户端库删除该参数,因为客户端库会自动将其设置为0(这就是为什么它声称不处理多个帐户)如果没有值,所以一种方法是将其覆盖为-1例如,这将显示多帐户选择器 . 然后,您还可以要求访问user's profile or email,同时您要求访问其他API并获取用户的电子邮件或其ID . 然后在随后的身份验证中,您可以指定绕过用户选择屏幕的 user_id 参数 .

    所以在实践中,首先授权如下:

    gapi.auth.authorize({client_id: <Your Client ID>,
                         scope: 'https://www.googleapis.com/auth/drive openid', // That requires access to Google Drive and to the UserInfo API
                         authuser: -1});
    

    上述问题的唯一问题是客户端库的自动刷新将无法工作,因为每个身份验证将在多帐户选择屏幕上被阻止 .

    诀窍是使用UserInfo API获取用户的ID,将该ID保存在会话cookie中,并在后续身份验证中使用它,如下所示:

    gapi.auth.authorize({client_id: <Your Client ID>,
                         scope: 'https://www.googleapis.com/auth/drive openid',
                         user_id: <The User ID>,
                         authuser: -1});
    

    指定用户ID将确保多帐户选择器被绕过,并允许从客户端lib自动刷新令牌再次工作 .

    作为参考,影响用户流的其他URL参数是:

    • user_id :类似于 authuser (绕过多帐户选择屏幕),但您可以使用电子邮件地址(例如bob@gmail.com)或您从我们的Open ID Connect endpoints / Google API / UserInfo API获取的用户ID

    • approval_prompt :默认为 auto ,可以设置为 force 以确保显示批准/授权屏幕 . 这可以确保在后续的auth(第一次之后)中不会绕过gant屏幕 .

    • immediateimmediate 有点棘手,当设置为 true 时,如果用户之前已经批准,它将绕过授权屏幕(有点像 approval_prompt=auto ),但如果用户之前未批准,则会重定向并显示错误: error=immediate_failed . 如果设置为 false ,则不会添加特殊行为,因此会通过 approval_prompt 值回退行为设置 .

    注意: immediate=trueapproval_prompt=force 是无效组合 .

    我认为客户端库正在使用 immediate param,这样如果他得到了 error=immediate_failed ,它将重新启动一个没有 authuser 参数的auth流程,但这只是推测:)

  • 0

    OAuth授权访问页面仅在不处于立即模式时显示,如果将 immediate 参数设置为false,它是否按预期工作?

  • 42

    根据http://code.google.com/p/google-api-javascript-client/issues/detail?id=11,Javascript客户端不支持多重登录

  • 3

    注意 authuser 参数 . 例如,将其设置为"2",即使您已经过身份验证,也会提示您登录 .

相关问题