Created
December 17, 2011 15:44
-
-
Save pomu0325/1490519 to your computer and use it in GitHub Desktop.
hacked FDC.ForceOAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var o = {}; | |
o.open = function(clientId) { | |
o.clientId = clientId; | |
FDC.ForceOAuth.open(clientId); | |
}; | |
o.apiVersion = FDC.ForceOAuth.apiVersion; | |
o.makeRestCall = function (path, callback, error, method, payload, retry) { | |
if (!o.instanceUrl) { | |
var oauthData = Ti.App.Properties.getString("oauthData_preference"); | |
var oauthJ = JSON.parse(oauthData); | |
o.instanceUrl = oauthJ.instance_url; | |
o.accessToken = oauthJ.access_token; | |
o.refreshToken = oauthJ.refresh_token; | |
} | |
var restUrl=Ti.Network.decodeURIComponent(o.instanceUrl)+'/services'+path; | |
var xhr=Ti.Network.createHTTPClient(); | |
xhr.onload=function(){ | |
Ti.API.info("REST Response: "+this.responseText); | |
var data=""; | |
if(this.responseText){data=this.responseText;} | |
callback(data); | |
}; | |
xhr.onerror=function(e){ | |
Ti.API.error("XHR, error handler..."+"\nDbDotCom.REST.OAuth.refreshToken: "+o.refreshToken+"\nretry: "+retry+"\n e: "+e.error+"\nXHR status: "+this.status);if(!o.refreshToken||retry){error(e.error); | |
}else{ | |
Ti.API.error("In the error handler looking for a 401, and have a "+xhr.status); | |
if(xhr.status===401){ | |
Ti.API.error("Handleing the 401 error..."); | |
o.refreshAccessToken(function(oauthResponse){ | |
Ti.API.error("Refresh response... "+oauthResponse); | |
o.makeRestCall(path,callback,error,method,payload,true); | |
},error); | |
}else{ | |
Ti.API.error("Not a 401 error, re-throwing...");error(e); | |
} | |
} | |
}; | |
xhr.open(method||"GET",restUrl,true) | |
Ti.API.info("Rest url: "+restUrl); | |
xhr.setRequestHeader("Authorization","OAuth "+Ti.Network.decodeURIComponent(o.accessToken)); | |
xhr.setRequestHeader("Content-Type","application/json"); | |
xhr.send(payload); | |
}; | |
o.refreshAccessToken = function(callback, onerror) { | |
var url = 'https://login.salesforce.com/services/oauth2/token'; | |
var qs = '?grant_type=refresh_token&client_id=' + o.clientId + '&refresh_token=' + o.refreshToken; | |
var xhr = Ti.Network.createHTTPClient(); | |
xhr.onload = function(){ | |
Ti.API.debug(xhr.responseText); | |
var j = JSON.parse(xhr.responseText); | |
j.refresh_token = o.refreshToken; | |
o.accessToken = j.access_token; | |
Ti.App.Properties.setString('oauthData_preference', JSON.stringify(j)); | |
FDC.ForceOAuth.setOAuthData(j); | |
callback(xhr.responseText); | |
}; | |
xhr.onerror = onerror; | |
xhr.open('POST', url + qs); | |
xhr.send(); | |
}; | |
o.query = function (soql, callback, error) {o.makeRestCall('/data/v'+o.apiVersion+'/query/?q='+escape(soql),callback,error,"GET");} | |
FDC.ForceOAuth2 = o; | |
})(); |
sorry, i forgot to mention that you need to use "makeRestCall" method directly.
like:
FDC.ForceOAuth2.makeRestCall('/data/v22.0/query/?...', function(e) {...});
client_secret no more needed for refresh.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the quick reply. I am still having issues with this thing. Below is the code that I am using with Dev Account in SF:
FDC = require('com.salesforce');
Ti.include('fdc-patch.js');
FDC.ForceOAuth2.open('3MVG9y6x0357HlefBtivwCfw64JZbW5NOvYNkQXcHY9H38fLihuJiCAE6mHuuZdzftJ_L_tuMhg==', '7990050930547381579');
FDC.ForceOAuth2.query("select id, name, billingCity, billlingState from Account", function(e) {
for (var i=0; i < e.records.length; i++)
{
tableData.push( Ti.UI.createTableViewRow({title:e.records[i].name}));
}
}, function(e) {
Ti.API.debug(e);
});
And here is Error Out put that I get:
[INFO] Salesforce/1.0 (2.0.1.GA2.999c68a)
[INFO] [object ComSalesforceModule] loaded
[INFO] OAuth Data String: {"access_token":"00DE0000000YCVH%21AQ0AQAm97TZ8Sja2Kl0pJmIM2ZGQ_RhDFWHwYaNZey5WLqyG05DOanmuQ8dYpBJEXwaLyKcSBWyDk6X_JwCiPPtiEDdt7JHh", "refresh_token":"5Aep861rEpScxnNE64xeqQ1jBsxOFB2rIxGXSMrNbE68PJhzSB0zvba.nZLWR8w94ol5KE07GwanA%3D%3D", "instance_url":"https%3A%2F%2Fna9.salesforce.com", "id":"https%3A%2F%2Flogin.salesforce.com%2Fid%2F00DE0000000YCVHMA4%2F005E0000000e8zQIAQ", "issued_at":"1337365120234", "signature":"TQYrlxR1eMvRnfWGuIK6ECV3WxWHGBOAouKZKwgJvmk%3D"}
[INFO] Calling oauth success event...https%3A%2F%2Fna9.salesforce.com
[INFO] Rest url: https://na9.salesforce.com/services/data/v22.0/query/?q=select%20id%2C%20name%2C%20billingCity%2C%20billlingState%20from%20Account
[ERROR] XHR, error handler...
DbDotCom.REST.OAuth.refreshToken: 5Aep861rEpScxnNE64xeqQ1jBsxOFB2rIxGXSMrNbE68PJhzSB0zvba.nZLWR8w94ol5KE07GwanA%3D%3D
retry: undefined
e: undefined
XHR status: 400
[ERROR] In the error handler looking for a 401, and have a 400
[ERROR] Not a 401 error, re-throwing...
Hope you can shed some light on to this.
Thanks,
Venkat