包含多个用户帐户的Google云端硬盘SDK OAuth2

时间:2016-08-25 10:47:52

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

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

enter image description here

我已设法使用这些代码和从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');
    });
}

我已经尝试了GoogleAuth.signIn()的{​​{3}}:

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

  • none:它没有进行身份验证
  • login:与select_account相同
  • consent:与select_account相同,它还要求离线使用权限...
  • select_account:与没有options

    的情况下登录相同的问题

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

  • 1 个答案:

    答案 0 :(得分: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);
            }
        });
    }