So you have already done this step:
Code:
function authenticate(){
var username = document.getElementById("username").value;
var pass = document.getElementById("password").value;
var params = "HOSTED_OR_GOOGLE&Email="+username+"@gmail.com&Passwd="+pass+"&service=cp&source=TestWidget_01";
sendAuthPostReq("https://www.google.com/accounts/ClientLogin", params);
}
function sendAuthPostReq(url, params){
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
} catch (e) {}
try{
if (!authenticateReq) {
authenticateReq = new XMLHttpRequest();
}else if (authenticateReq.readyState != 0){
authenticateReq.abort();
writelog("auth aborted");
}
authenticateReq.open("POST", url, true);
authenticateReq.onreadystatechange = handleAuthResponse;
authenticateReq.overrideMimeType("application/x-www-form-urlencoded");
authenticateReq.setRequestHeader("Content-length", params.length);
authenticateReq.setRequestHeader("Connection", "close");
authenticateReq.send(params);
writelog("Auth POST sent");
}
catch(e){
writelog("ajaxrequest sending error "+e);
}
}
function handleAuthResponse(){
if(authenticateReq.readyState == 4) {
writelog("4 (Loaded) All data received. responseTex/responseBody available.");
if (authenticateReq.status == 200){
writelog("Auth response got");
var authTokenIndex = authenticateReq.responseText.indexOf("Auth=");
if(authTokenIndex != -1){
var authToken = authenticateReq.responseText.substr(authTokenIndex+5,authenticateReq.responseText.length);
authToken = authToken.replace(/^\s+|\s+$/g,""); //to get rid of the extra space at the end
alert(authToken);
}
}else{
writelog("Authenticate error: " + authenticateReq.status+ " "+ authenticateReq.statusText);
}
}
}
Then you just have to set the header
Code:
var url = "http://www.google.com/m8/feeds/contacts/default/full";
var auth = "GoogleLogin auth=D..YOUR AUTH..o";
sendajaxrequest(url, auth);
function sendajaxrequest(url, auth){
writelog("ajaxrequest setup started");
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
} catch (e) {}
try{
if (!ajaxrequest) {
ajaxrequest = new XMLHttpRequest();
}else if (ajaxrequest.readyState != 0){
ajaxrequest.abort();
writelog("ajaxrequest aborted");
}
ajaxrequest.open("GET", url, true);
ajaxrequest.onreadystatechange = handleAjaxResponse;
ajaxrequest.setRequestHeader("Content-type", "application/atom+xml");
ajaxrequest.setRequestHeader("Authorization", auth);
ajaxrequest.send(null);
writelog("ajaxrequest GET sent");
}
catch(e){
writelog("ajaxrequest sending error "+e);
}
}
For some reason you will get SYNTAX_ERR: DOM Exception 12 if you try to call sendajaxrequest directly from the handleAuthResponse.
However it works if you first login and then get the contacts by using stored auth token.
Ps.
The script presented does not handle the login event from top to bottom. For example CAPTCHA handling is omitted.
-Ilkka