I am able to get the access_token for the salesforce oath2. access token can expire after some time and after that we need to refresh the access token. But I am always getting “Bad Request” error in this code.
function getRefreshToken(refreshToken) {
var url = loginUrl + 'token';
var client = Ti.Network.createHTTPClient({
// // function called when the response data is available
onload : function(e) {
Ti.API.info("Received text: " + this.ResponseText);
alert(this.status);
alert(this.responseText);
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
alert(this.status);
alert(e.error);
},
timeout : 5000 //in milliseconds
});
//Prepare the connection.
client.open("POST", url);
client.setRequestHeader("content-type", "application/json");
//Send the request.
var param = 'grant_type=refresh_token&client_id=' + escape(clientId) + '&client_secret='+ escape(clientSecret) + '&refresh_token=' + escape(refreshToken);
Ti.API.info(param);
client.send(param);
}
am I missing something in this code? expected result is a json response with new access_token.
What the exact URL you send the request to? Also, you set the request content-type to be json, but seem to be sending a forms request, not a json formatted request, you probably need to change the content-type header to be “application/x-www-form-urlencoded”