Partners, who wish to get an OAuth consumer Id for authentication, can contact salesforce.com
Setting up OAuth 2.0 requires that you take some steps within Salesforce and in other locations. If any of the steps are unfamiliar, see Understanding Authentication or the Salesforce online help. The following example uses the Web server OAuth flow.
In your client application, redirect the user to the appropriate Salesforce authorization endpoint. On successful user login, Salesforce will call your redirect URI with an authorization code. You use the authorization code in the next step to get the access token.
initParams = { @WebInitParam(name = "clientId", value = "3MVG9lKcPoNINVBJSoQsNCD.HHDdbugPsNXwwyFbgb47KWa_PTv"), @WebInitParam(name = "clientSecret", value = "5678471853609579508"), @WebInitParam(name = "redirectUri", value = "https://localhost:8443/RestTest/oauth/_callback"), @WebInitParam(name = "environment", value = "https://na1.salesforce.com/services/oauth2/token") } HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(environment); post.addParameter("code",code); post.addParameter("grant_type","authorization_code"); /** For session ID instead of OAuth 2.0, use "grant_type", "password" **/ post.addParameter("client_id",clientId); post.addParameter("client_secret",clientSecret); post.addParameter("redirect_uri",redirectUri);
If the value of client_id (or consumer key) and client_secret (or consumer secret) are valid, Salesforce sends a callback to the URI specified in redirect_uri that contains a value for access_token.
//exception handling removed for brevity...
//this is the post from step 2
httpclient.executeMethod(post);
String responseBody = post.getResponseBodyAsString();
String accessToken = null;
JSONObject json = null;
try {
json = new JSONObject(responseBody);
accessToken = json.getString("access_token");
issuedAt = json.getString("issued_at");
/** Use this to validate session
* instead of expiring on browser close.
*/
} catch (JSONException e) {
e.printStackTrace();
}
HttpServletResponse httpResponse = (HttpServletResponse)response;
Cookie session = new Cookie(ACCESS_TOKEN, accessToken);
session.setMaxAge(-1); //cookie not persistent, destroyed on browser exit
httpResponse.addCookie(session);
This completes the authentication.
HttpClient httpclient = new HttpClient();
GetMethod gm = new GetMethod(serviceUrl);
//set the token in the header
gm.setRequestHeader("Authorization", "Bearer "+accessToken);
//set the SOQL as a query param
NameValuePair[] params = new NameValuePair[1];
/**
* other option instead of query string, pass just the fields you want back:
* https://instance_name.salesforce.com/services/data/v20.0/sobjects/Account/
* 001D000000INjVe?fields=AccountNumber,BillingPostalCode
*/
params[0] = new NameValuePair("q","SELECT name, title FROM Contact LIMIT 100");
gm.setQueryString(params);
httpclient.executeMethod(gm);
String responseBody = gm.getResponseBodyAsString();
//exception handling removed for brevity
JSONObject json = new JSONObject(responseBody);
JSONArray results = json.getJSONArray("records");
for(int i = 0; i < results.length(); i++)
response.getWriter().write(results.getJSONObject(i).getString("Name")+ ",
"+results.getJSONObject(i).getString("Title")+"\n");
curl https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=myclientid" -d "client_secret=myclientsecret" -d "mylogin@salesforce.com"
-d "password=mypassword123456"Authorization: Bearer access_token
For example:
curl https://instance_name.salesforce.com/services/data/v20.0/ -H 'Authorization: Bearer access_token'