tags:

views:

29

answers:

2

Hi,

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

+3  A: 
patrick dw
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
@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.
patrick dw
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
@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)
patrick dw
A: 

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

Aaron Saunders
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.
anchnk