2012-07-14 75 views
1

我試圖在jQuery/ajax函數中觸發Google plus connect API。 我得到的迴應是未定義的。 Firebug顯示一個空的迴應。Google Plus使用jQuery/Ajax連接API

這是代碼我使用:

function gPlusConnect() 
{ 
    console.debug('line 401 ajax gestartet'); 
    result=$.ajax({ 
     type: 'POST', 
     async: false, // false 
     url: 'https://accounts.google.com/o/oauth2/auth', 
     data: ({ 
      scope:'https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile', 
      state:'/profile', 
      redirect_uri:'https://ssl.webpack.de/learnface.de/donation/launchpages', 
      response_type:'code', 
      client_id:'.........82.apps.googleusercontent.com', 
     }) 
    }).responseText; 
    console.debug('index.php 415 ajax ends with '+result+' from g+'); 
} 

結果在Firebug是:

Header  Post  Antwort HTML 
index.php 415  ajax ends undefined from g+ 

有沒有人成功地做到了這一點?你推薦一個更好的環境嗎?

回答

2

在官方客戶端庫上使用Google OAuth 2.0更容易。授權用戶通過這種方式與JavaScript客戶端庫是很容易的:

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script> 
<script> 
function handleClientLoad() { 
    gapi.client.setApiKey(apiKey); 
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); 
} 
function handleAuthResult(authResult) { 
    console.log(authResult); 
} 
</script> 

您可以在源代碼樹更detailed sample客戶端庫,並更多地瞭解圖書館在其code hosting project