gapi.auth2.getAuthInstance()。isSignedIn.listen()不起作用

时间:2016-08-10 10:41:08

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

我在google drive api身份验证中使用OAuth2.0。我有一个isSignedIn监听器,其afterSignIn作为回调 登录后我的问题:afterSignIn()功能未被触发。有人知道如何解决这个问题吗?

function googleDriveAuthentication($rootScope){
    var API_KEY = '...'
    var CLIENT_ID = '...';
    var SCOPES = 'https://www.googleapis.com/auth/drive';
    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.isSignedIn.listen(afterSignIn);
            auth2.signIn();
        });
    }
    function afterSignIn(){
        console.log('authenticated successfully');
        $rootScope.authenticated = true;
        $rootScope.$broadcast('authenticated');
        gapi.client.load('drive', 'v3');
    }
}

2 个答案:

答案 0 :(得分:2)

这里afterSignIn是一个监听器函数,listener是一个取一个布尔值的函数。 listen()在用户登录时将true传递给true函数,并在用户注销时传递false。

这里你的函数应该有一个参数。请参阅此文档custom helper

// Listen for sign-in state changes.
auth2.isSignedIn.listen(afterSignIn);

您必须将侦听器功能更改为

var afterSignIn = function (val) {
    console.log('Signin state changed to ', val);
    $rootScope.authenticated = val;
    if(val == true){
    $rootScope.$broadcast('authenticated');
    gapi.client.load('drive', 'v3');
   }
};

答案 1 :(得分:1)

.then(...)中,您可以获取isSignedIn的状态:

var btnGoogleLogin = $('#btnGoogleLogin');

gapi.load('auth2', function () {
    auth2 = gapi.auth2.init({
        client_id: 'Google_AUTH_ClientID',
        cookiepolicy: 'single_host_origin'
    }).then(function (authResult) {
        var signedIn = authResult.isSignedIn.get();
        if (signedIn) {
            //Call signedInCallback yourself
            signedInCallback(signedIn);
        } else {
            //Get Callback when Login With Google is clicked and authenticated
            authResult.isSignedIn.listen(signedInCallback);
            
            authResult.attachClickHandler(btnGoogleLogin[0], {}, function (currentUser) {
                //No need to do anything here with currentUser, signedInCallback will be called.
            }, function (error) {
                //Will also get error if the user just closes the popup
                console.log('GAuth Error:', error);
            });
        }

        function signedInCallback(signedIn) {
            console.log('signInCallback', signedIn);

            if (signedIn) {
                var currentUser = authResult.currentUser.get();

                var authResponse = currentUser.getAuthResponse();
                console.log('authResponse', authResponse);
                //Get Token from authResponse
                
                var basicProfile = currentUser.getBasicProfile();
                console.log('basicProfile', basicProfile);
                //Get Email, Name, etc. from basicProfile

                btnGoogleLogin.html("Logged in: <b>" + basicProfile.getName() + "</b>");
                //Post the details to your back-end with ajax or whatever you use
                //Redirect the user
            } else {
                btnGoogleLogin.html("Login with Google");
            }
        }
    });
});