Verify the REST Environment (REST-Based APIs)

You can verify your developer environment with a simple Java test application in Eclipse.

You should have the JDK, Eclipse, and the HttpClient and JSON frameworks installed.

  1. Run Eclipse. Click File | New | Java Project and name the project “SF-REST-Test.”

  2. Click Project | Properties | Java Build Path | Libraries and click Add External JARs. Add the HttpClient .jar files: httpclient, httpcore, commons-codec, and commons-logging (the .jar files will have version information in the filenames). Add the JSON .jar file, which might also have a version number in the .jar filename.

  3. Add a new folder to the src folder by right-clicking src, then select New | Folder and use sfdc_rest as the folder name.

  4. Create a new class by right-clicking sfdc_rest and selecting New | Class. Name the class Main.

  5. Replace the code Eclipse generates for Main.java as described in the following section.

Use the following simple login example code for your Main.java class. Replace YOUR DEVORG USERNAME with your developer organization username, and replace YOUR DEVORG PASSWORD + SECURITY TOKEN with your developer organization password appended with your security token. If you did not set a security token in your organization, just provide your password. Replace YOUR OAUTH CONSUMER KEY with the consumer key from your development organization’s connected app. Replace YOUR OAUTH CONSUMER SECRET with the consumer secret from your development organization’s connected app. A GitHub Gist of this code is available here: https://gist.github.com/anonymous/fcb1bc36ef50c0efbeb5.

1package sfdc_rest;
2
3import java.io.IOException;
4
5import org.apache.http.client.methods.HttpPost;
6import org.apache.http.impl.client.DefaultHttpClient;
7import org.apache.http.HttpResponse;
8import org.apache.http.HttpStatus;
9import org.apache.http.util.EntityUtils;
10import org.apache.http.client.ClientProtocolException;
11
12import org.json.JSONObject;
13import org.json.JSONTokener;
14import org.json.JSONException;
15
16public class Main {
17
18    static final String USERNAME     = "YOUR DEVORG USERNAME";
19    static final String PASSWORD     = "YOUR DEVORG PASSWORD + SECURITY TOKEN";
20    static final String LOGINURL     = "https://login.salesforce.com";
21    static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
22    static final String CLIENTID     = "YOUR OAUTH CONSUMER KEY";
23    static final String CLIENTSECRET = "YOUR OAUTH CONSUMER SECRET";
24
25    public static void main(String[] args) {
26
27        DefaultHttpClient httpclient = new DefaultHttpClient();
28
29        // Assemble the login request URL
30        String loginURL = LOGINURL + 
31                          GRANTSERVICE + 
32                          "&client_id=" + CLIENTID + 
33                          "&client_secret=" + CLIENTSECRET +
34                          "&username=" + USERNAME +
35                          "&password=" + PASSWORD;
36
37        // Login requests must be POSTs
38        HttpPost httpPost = new HttpPost(loginURL);
39        HttpResponse response = null;
40
41        try {
42            // Execute the login POST request
43            response = httpclient.execute(httpPost);
44        } catch (ClientProtocolException cpException) {
45            // Handle protocol exception
46        } catch (IOException ioException) {
47            // Handle system IO exception
48        }
49
50        // verify response is HTTP OK
51        final int statusCode = response.getStatusLine().getStatusCode();
52        if (statusCode != HttpStatus.SC_OK) {
53            System.out.println("Error authenticating to Force.com: "+statusCode);
54            // Error is in EntityUtils.toString(response.getEntity()) 
55            return;
56        }
57
58        String getResult = null;
59        try {
60            getResult = EntityUtils.toString(response.getEntity());
61        } catch (IOException ioException) {
62            // Handle system IO exception
63        }
64        JSONObject jsonObject = null;
65        String loginAccessToken = null;
66        String loginInstanceUrl = null;
67        try {
68            jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
69            loginAccessToken = jsonObject.getString("access_token");
70            loginInstanceUrl = jsonObject.getString("instance_url");
71        } catch (JSONException jsonException) {
72            // Handle JSON exception
73        }
74        System.out.println(response.getStatusLine());
75        System.out.println("Successful login");
76        System.out.println("  instance URL: "+loginInstanceUrl);
77        System.out.println("  access token/session ID: "+loginAccessToken);
78
79        // release connection
80        httpPost.releaseConnection();
81    }
82}

The following example output shows a typical successful run of this code.

1HTTP/1.1 200 OK
2Successful login
3  instance URL: https://yourInstance.salesforce.com
4  access token/session ID: 00DU0000000L5SPxa1XFi0rwB16YCQ.Xyv2nKiCT8iIN9_nkKQJ3UUf

If the verification Java project runs and displays output that matches your organization, your developer environment is now set up and you can start developing Java applications that integrate with Salesforce REST APIs.