user1525277
user1525277

Reputation: 11

Google Plus connect API using jQuery/Ajax

I am trying to trigger the Google plus connect API within a jquery/ajax function. The response I get is undefined. Firebug shows an empty response.

This is the code I am using:

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

The result in firebug is:

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

Has anyone done this successfully? Do you recommend a better alernative?

Upvotes: 1

Views: 3877

Answers (1)

mimming
mimming

Reputation: 13954

It's a lot easier to use OAuth 2.0 on Google with the official client libraries. Authorizing users this way with the JavaScript client library is really easy:

<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>

You can find a more detailed sample in the source code tree for the client library, and learn more about the library on its code hosting project.

Upvotes: 2

Related Questions