Skip to content

Instantly share code, notes, and snippets.

@pfeilbr
Created February 12, 2012 19:57
Show Gist options
  • Save pfeilbr/1810500 to your computer and use it in GitHub Desktop.
Save pfeilbr/1810500 to your computer and use it in GitHub Desktop.
Patch to forcetk.js to login & apexrest methods. add login via username & password with a grant_type of password to eliminate the Oauth redirect process
var c = new forcetk.Client('<client_id>');
c.login({
'environment': 'test',
'client_id': '<client_id>',
'client_secret': '<client_secret>',
'username': 'joe@example.com',
'password': 'secret123'
}, function(data) {
c.describeGlobal(function(data) {
console.log(JSON.stringify(data));
});
c.apexrest('/communications',
function(data) {
console.log(JSON.stringify(data));
},
function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
}
);
}, function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
});
/*
options['environment'] - 'production' or 'test'
options['client_id']
options['client_secret']
options['username']
options['password']
*/
forcetk.Client.prototype.login = function(options, callback, error) {
var that = this;
var optionKeys = 'environment,client_id,client_secret,username,password';
var savedSessionKey = '';
$.each(optionKeys.split(','), function(i, e) {
savedSessionKey += options[e];
});
var successFn = function(data) {
that.setSessionToken(data['access_token'], null, data['instance_url'])
localStorage[savedSessionKey] = JSON.stringify(data);
callback(data);
};
var sessionAsJson = localStorage[savedSessionKey];
if (sessionAsJson) {
successFn(JSON.parse(sessionAsJson));
return;
}
$.ajax({
type: "POST",
async: true,
url: 'https://' + ((options['environment'] === 'production') ? 'login' : 'test') + '.salesforce.com/services/oauth2/token',
contentType: 'application/x-www-form-urlencoded',
cache: false,
processData: true,
data: $.extend(options, {
'grant_type': 'password'
}),
success: successFn,
error: error,
dataType: "json",
});
};
forcetk.Client.prototype.apexrest = function(path, callback, error, method, payload, retry) {
var that = this;
var url = this.instanceUrl + '/services/apexrest' + path;
$j.ajax({
type: method || "GET",
async: this.asyncAjax,
url: (this.proxyUrl !== null) ? this.proxyUrl: url,
contentType: 'application/json',
cache: false,
processData: false,
data: payload,
success: callback,
error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status === 401) {
that.refreshAccessToken(function(oauthResponse) {
that.setSessionToken(oauthResponse.access_token, null,
oauthResponse.instance_url);
that.ajax(path, callback, error, method, payload, true);
},
error);
} else {
error(jqXHR, textStatus, errorThrown);
}
},
dataType: "json",
beforeSend: function(xhr) {
if (that.proxyUrl !== null) {
xhr.setRequestHeader('SalesforceProxy-Endpoint', url);
}
xhr.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);
xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);
if (that.userAgentString !== null) {
xhr.setRequestHeader('User-Agent',that.userAgentString);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment