Skip to content

Instantly share code, notes, and snippets.

/Main.java Secret

Created February 10, 2014 20:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fcb1bc36ef50c0efbeb5 to your computer and use it in GitHub Desktop.
Save anonymous/fcb1bc36ef50c0efbeb5 to your computer and use it in GitHub Desktop.
Salesforce Java Developer Environment - Verify REST Environment
package sfdc_rest;
import java.io.IOException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
public class Main {
static final String USERNAME = "YOUR DEVORG USERNAME";
static final String PASSWORD = "YOUR DEVORG PASSWORD + SECURITY TOKEN";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
static final String CLIENTID = "YOUR OAUTH CONSUMER KEY";
static final String CLIENTSECRET = "YOUR OAUTH CONSUMER SECRET";
public static void main(String[] args) {
DefaultHttpClient httpclient = new DefaultHttpClient();
// Assemble the login request URL
String loginURL = LOGINURL +
GRANTSERVICE +
"&client_id=" + CLIENTID +
"&client_secret=" + CLIENTSECRET +
"&username=" + USERNAME +
"&password=" + PASSWORD;
// Login requests must be POSTs
HttpPost httpPost = new HttpPost(loginURL);
HttpResponse response = null;
try {
// Execute the login POST request
response = httpclient.execute(httpPost);
} catch (ClientProtocolException cpException) {
// Handle protocol exception
} catch (IOException ioException) {
// Handle system IO exception
}
// verify response is HTTP OK
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Error authenticating to Force.com: "+statusCode);
// Error is in EntityUtils.toString(response.getEntity())
return;
}
String getResult = null;
try {
getResult = EntityUtils.toString(response.getEntity());
} catch (IOException ioException) {
// Handle system IO exception
}
JSONObject jsonObject = null;
String loginAccessToken = null;
String loginInstanceUrl = null;
try {
jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
loginAccessToken = jsonObject.getString("access_token");
loginInstanceUrl = jsonObject.getString("instance_url");
} catch (JSONException jsonException) {
// Handle JSON exception
}
System.out.println(response.getStatusLine());
System.out.println("Successful login");
System.out.println(" instance URL: "+loginInstanceUrl);
System.out.println(" access token/session ID: "+loginAccessToken);
// release connection
httpPost.releaseConnection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment