【问题标题】:Google Drive SDK OAuth2 with multiple user account具有多个用户帐户的 Google Drive SDK OAuth2
【发布时间】:2016-08-25 10:47:51
【问题描述】:

如果用户在其 Google 中有多个帐户,则用户必须选择一个帐户,因为它不记得用户之前选择了哪个帐户。



我已经设法使用这些代码和 https://developers.google.com/api-client-library/javascript/features/authentication#specifying-your-client-id-and-scopes 指导的配置进行 OAuth2 身份验证

this.authenticate = function(){
    gapi.load('client:auth2',authorize);
}
function authorize(){
    gapi.client.setApiKey(API_KEY);
    gapi.auth2.init({
        client_id: CLIENT_ID,
        scope: SCOPES
    }).then(function(authResult){
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signIn().then(afterSignIn);
    });
}
function afterSignIn(){
    console.log('authenticated successfully');
    $rootScope.authenticated = true;
    gapi.client.load('drive', 'v3',function(){
        $rootScope.$broadcast('authenticated');
    });
}

这些optionsGoogleAuth.signIn() 我都试过了:

auth2.signIn({
            'prompt': '**promtOption**'
  })...

  • none :它不进行身份验证
  • login:与​​select_account 相同
  • consent:和select_account一样,还需要离线使用权限...
  • select_account: 与不使用options 登录的问题相同

    如何让程序记住用户的选择?

  • 【问题讨论】:

      标签: javascript google-drive-api google-oauth


      【解决方案1】:

      调用auth2.signIn() 将始终提示用户登录,即使他们已经登录。在此之前,请检查他们是否已经使用auth2.currentUser.get().isSignedIn() 登录。这是您的代码的修改版本:

      function authorize(){
          gapi.client.setApiKey(API_KEY);
          gapi.auth2.init({
              client_id: CLIENT_ID,
              scope: SCOPES
          }).then(function(authResult){
              var auth2 = gapi.auth2.getAuthInstance();
              var user = auth2.currentUser.get();
              if (user.isSignedIn()) {
                afterSignIn();
              } else {
                auth2.signIn().then(afterSignIn);
              }
          });
      }
      

      【讨论】:

      • 完美答案。我不知道 OAuth2.0 为您处理一切!!!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2014-02-20
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多