2012-07-14 73 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