2014-06-10 2 views
28

내 코드에 버그가 있습니다. 로그인하여 사용자 정보를 검색 할 수 있습니다. 그러나 signinCallback이 다시 호출됩니다 (나는 방법을 모른다). 그리고 이전에 사라진 사용자 정보를 보여줍니다! 여기 HTML 측이다Google+ signin 콜백이 두 번 호출되어 프로세스에서 authresult가 손실됩니다.

<span id="signinButton"> 
    <span 
     class="g-signin" 
     data-callback="signinCallback" 
     data-clientid="CLIENT_ID" 
     data-cookiepolicy="single_host_origin" 
     data-requestvisibleactions="http://schemas.google.com/AddActivity" 
     data-scope="https://www.googleapis.com/auth/plus.profile.emails.read" 
     data-width="standard" 
     data-height="short"> 
    </span> 
</span> 

여기 자바 스크립트 측이다

var AuthStates = { 
    google: null 
}; 

function signinCallback(authResult) { 
    console.dir(authResult); 
    console.log('Sign-in state: ' + authResult['error']+authResult['access_token']); 
    AuthStates.google = authResult; 
    console.log('signinCallback'); 
    chooseAuthProvider(); 
} 

function chooseAuthProvider() { 
    if (AuthStates.google && AuthStates.facebook) { 
    if (AuthStates.google['access_token']) { 
     // Signed in with Google, you can now use Google+ APIs. 
     console.log(AuthStates.google); 
     gapi.client.load('plus','v1', function(){ 
     var request = gapi.client.plus.people.get({ 
      'userId': 'me' 
     }); 
     request.execute(function(resp) { 
      document.getElementById('cname').value =resp.displayName; 
      document.getElementById('cemail').value =resp.emails[0].value; 
      console.log('Retrieved profile for:' + resp.displayName + ' ' + resp.emails[0].value); 
     }); 
     }); 
} 
} 

그것은 제 signinCallback

Sign-in state: user_signed_outundefined 
signinCallback 

Result of console.dir(authResult)

+0

. [여기] (http://stackoverflow.com/questions/24056169/google-signincallback-called-twice-when-user-is-signed-out)과 비슷한 문제가 있습니다. [Google + Dev Community] (https://plus.google.com/comm unities/113527920160449995981). signInCallback이 호출 된 직후에'console.dir (authResult)'가 출력하는 것을 캡쳐 해 주시겠습니까? 특히 두 호출 모두에서'authResult [ 'status'] [ 'method']'의 값은 무엇입니까? –

+0

authResult [ 'status'] [ 'method'] = null – user3528581

+0

글쎄, 우리는 똑같은 문제가있다. 여러 Google 계정을 사용하고 있습니까? –

답변

1
의 콘솔이 응답을 준다

귀하의 request.execute() 호출 콜백 메소드가 error 속성의 "user_signed_out"값으로 콜백 메소드를 다시 트리거하도록합니다.

은 Google 문서 "Signing out the user"가 읽어 봐 걸릴 경우 사용자가 페이지를 새로 고침하거나 웹 사이트의 다른 부분으로 이동할 때, 콜백이 오류 user_signed_out 값으로 실행됩니다

을 사용자가 로그인 버튼을 다시 클릭 할 때까지

따라서 콜백 메소드에 대한 두 번째 호출을 트리거하는 request.execute() 호출이 필요하다고 생각합니다.

콜백 메소드 내에 조건을 넣어 두 번째 콜백 호출을 방지 할 수 있습니다. 앞서 언급 한 가드 조건의 예를 들어 "Monitoring the user's session state"

function signinCallback(authResult) { 
if (authResult['status']['signed_in']) { 
    console.dir(authResult); 
    console.log('Sign-in state: ' + authResult['error']+authResult['access_token']); 
    AuthStates.google = authResult; 
    console.log('signinCallback'); 
    chooseAuthProvider(); 
} 
} 

페이지 구글의 문서.

0

이에 대한 당신

(함수() { var에 GOOGLE_PLUS_SCRIPT_URL = 'https://apis.google.com/js/client:plusone.js'도움이 될 수 있습니다;

window.oauth2Callback = function(authResult) { 
if (authResult['access_token']) { 
    accessToken = authResult['access_token']; 
    $(function() { 
$.getScript(GOOGLE_PLUS_SCRIPT_URL);} 
관련 문제