2016-08-10 6 views
1

Google 드라이브 API 인증에 OAuth2.0을 사용하고 있습니다. 콜백으로 afterSignIn을 가진 isSignedIn 수신기가 있습니다.
문제 : 로그인 후 afterSignIn() 기능이 실행되지 않습니다. 누군가 이것을 고치는 방법을 알고 있습니까?gapi.auth2.getAuthInstance(). isSignedIn.listen()가 작동하지 않습니다.

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'); 
    } 
} 

답변

1

여기 afterSignIn 청취자는 부울 값을 취하는 함수이다 리스너 함수이다. listen()은 사용자가 로그인 할 때이 함수에 true를 전달하고 사용자가 로그 아웃 할 때 false를 전달합니다.

여기에 함수에 매개 변수가 있어야합니다. 이 문서 https://developers.google.com/identity/sign-in/web/listeners

// 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'); 
    } 
}; 
에 리스너 함수를 변경해야합니다