REST API with oAuth
Problem Statement
The Force.com REST API provides you with a powerful, convenient, and simple Web services API for interacting with Force.com.
To use the REST API, it is essential to have valid Access token/Session Id for each call but SFDC documentation does not explain how to get access-token using username/password with the oAuth.
Following solution explains how to obtain the access token using username/password and fetching the profile details along with User count.
Advantage with REST API is does not have SOQL governor limit with the Aggregate query.
Solution
Step1: Create Remote Access Setting into SFDC
Step2: Create Token Request by using username/password
Javascripts
Var authinfo ;
Ext.Ajax.request({
url : 'https://test.visualstudiocoading.blogspot.com/services/oauth2/token',
method : 'POST',
async:false,
params : {
grant_type : 'password',
client_id: '3MVG9FS3IyroMOh5h26EVC_527mrgPtXMObqfIKg2vd5xdClvP2_I0kIEOAIk3MgZ0Yxz6Giui9ozkKWuUiNQ', // Consumer Key
client_secret: '4323324214295565712', // Consumer Secret
username : username, //UserName
password : password //Password (if your IP address is not white listed then user Security token with the password)
},
success : function(response) {
console.log(response.responseText);
authInfo = response.responseText;
},
fail:function(response){
status=false;
}
});
This will return instance url and Session key that are used into next REST API call.
1. instance_url
2. access_token
Step3: REST CALL to fetch Profiles name with user count.
Ext.Ajax.request({
url: authinfo.instance_url + '/services/data/v24.0/query?q=select+count(id)+userCount,+profile.name+from+user+group+by+profile.name' ,
method: 'GET',
async:false,
headers: { 'Content-Type' : 'application/json', 'Authorization' : 'OAuth ' authinfo.access_token },
success: function(response){
var text = response.responseText;
},
fail: function(response){
Ext.Msg.alert('Error!','Some Error in Loading profiles.',Ext.emptyFn);
}
});
Note: In the above example we have used EXTJS.
0 Comments