2

I'm currently messing with google's apis using ajax trough jquery library. In order to use a google service API i need to get an auth token sending a request to ClientLogin. Actually, i've no idea how to pass the token to the second request .

I've set a global variable token as var token = null;

I call two requests on $(document).ready event.

  1. The first one is an https POST request to google clientLogin in order to get user credential token.

Here's the code of the first ajax request :

$.ajax({
    type: "POST",
    url: "https://" + host + clientLoginEntryPoint,
    data: cLRequestData(accountType, user, pwd, service),
    dataType: "html",
    success: function (response) {      
        var tokenArray = response.split("="); // Split to response tokenArray[3] is the auth token
        token = tokenArray[3];
        $(".status").html(token);
    }
}); // END OF CLIENT LOGIN REQUEST
  1. The second one is supposed to call the contacts API with an http GET.

    $.ajax({
    type:  "GET",
    url: "http://" + host + googleContactEntryPoint,
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token);
        xhr.setRequestHeader('GData-Version', '3.0');
    },
    success: function(response, textStatus, xhr) {
        var names = $(response).find('entry>title').text();
        $(".status").text(names);
    },
    error: function(xhr, status, error) {
        $(".status").html(xhr.status+ " "+ xhr.statusText );
    }
    

    }); // END OF GOOGLE CONTACT REQUEST

The problem i'm facing is that token is set to null when i try to set Google Authentification Header in the second request. I've already read link text but this is not working for me. I now that must be something to do with callback/events but i'm couldn't figure how to do this.

Any help appreciated

2 Answers 2

3

Question was updated:

You are redeclaring a local variable in your first request named token, so it doesn't use the global token that you initiated with null.

Remove the var keyword in the first request.

token = tokenArray[3];

If the requests run sequentially, then the second will not wait for the first to receive its response before it executes.

If that's the case, place the second request in a function, and call that function from the success: callback in the first.

$.ajax({
    type: "POST",
    url: "https://" + host + clientLoginEntryPoint,
    data: cLRequestData(accountType, user, pwd, service),
    dataType: "html",
    success: function (response) {      
        var tokenArray = response.split("="); // Split to response tokenArray[3] is the auth token
        token = tokenArray[3];
        $(".status").html(token);
        // Call second request here
        secondRequest( token );  // optionally pass the "token"
                                 //   instead of using global var
    }
}); // END OF CLIENT LOGIN REQUEST


function secondRequest( token ) {
    $.ajax({
    type:  "GET",
    url: "http://" + host + googleContactEntryPoint,
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token);
        xhr.setRequestHeader('GData-Version', '3.0');
    },
    success: function(response, textStatus, xhr) {
        var names = $(response).find('entry>title').text();
        $(".status").text(names);
    },
    error: function(xhr, status, error) {
        $(".status").html(xhr.status+ " "+ xhr.statusText );
    }
    })
}

(In this code, you could actually get rid of the global token variable, and just pass the token from the first request to the second as a function parameter.)

Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks Patrick for your answer. I've updated code but my script broke when i call the secondRequest. It break when i'm setting the Authorization header to the token value. I've got the following console error: Error: uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXMLHttpRequest.setRequestHeader]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: GContactsAPI.js :: anonymous :: line 45" data: no] Any ideas ?
@anchnk - Have you verified that token looks correct inside of secondRequest()? I don't know anything about the response you're getting from the first request, or how to properly set up your second. I'd do console.log( token ) in secondRequest() function to make sure it has the value you expect.
Yes i got the same value than in the full response. I'll try to restart browser and see. Another point is that i'm doing that request from a local file from now. So i added netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); to be able to send request but i'm not sure it have something to do with that.
@anchnk - I'm just not sure. I guess I'd "Accept" this answer (click the checkmark to the left of it) and start a new question that describes the issue since it a little bit beyond the original question. :o)
0

looks like the scope of the variable token is not global. you have it defined in the scope of the success function for the initial ajax request

1 Comment

Sorry that was an error during copy/pasting i've edited the post to reflect the current version of the source, it isn't working without the var keyword.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.