2015-07-19 100 views
1

我跟着tutorial by google但我有問題。回調函數永遠不會被調用。 的代碼在本地主機 跑到這是我的代碼:谷歌登錄服務器端

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script> 
    <script> 
     function start() { 
      gapi.load('auth2', function() { 
       auth2 = gapi.auth2.init({ 
        client_id: '<my-client-id>', 
        scopes: ['https://www.googleapis.com/auth/calendar'] 
       }); 
      }); 
     } 
     function signInCallback(authResult) { 
      document.write("here"); 
      if (authResult['code']) { 
       document.write("code is good"); 
      } else { 
       document.write("error"); 
      } 
     } 
    </script> 
    <meta charset="UTF-8"> 
    <title></title> 
</head> 
<body> 
<button id="signinButton">Sign in with Google</button> 
<div id="result"></div> 
<script> 
    $('#signinButton').click(function() { 
     auth2.grantOfflineAccess({'redirect_uri': 'postmessage'}).then(signInCallback); 
    }); 
</script> 
</body> 
</html> 

它在本教程中提到,但我沒有得到document.write我把回調函數。 另外,有沒有辦法可以在登錄成功後擁有用戶的用戶名?

+0

嘗試登錄的結果相反,'的console.log(authResult);',並檢查輸出在開發工具中(按F12)。有沒有迴應? – Tholle

+0

試過了,什麼都沒有。這就像它從來沒有得到這個功能 – Javi

+1

嗯,令人沮喪。您確定您已將'http:// localhost:8080'分配給Developers Console中的'JavaScript origins',並刪除了重定向網址嗎? https://console.developers.google.com/ – Tholle

回答

1

看起來不錯。請確保您將http://localhost:8080分配給Developers Console中的JavaScript Origins,並且您已將Redirect URL刪除。

一旦你得到的工作,所有你需要做的就是將USERPROFILE如下要求:

var request = gapi.client.plus.people.get({ 
    'userId': 'me' 
}); 
request.execute(function(resp) { 
    console.log('Retrieved profile for:' + resp.displayName); 
});