Advertisement
Guest User

Untitled

a guest
Oct 6th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. import java.io.IOException;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.impl.client.HttpClientBuilder;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.HttpStatus;
  11. import org.apache.http.util.EntityUtils;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.json.JSONObject;
  14. import org.json.JSONTokener;
  15. import org.json.JSONException;
  16.  
  17. public class OrderProcessing extends HttpServlet {
  18. private static final long serialVersionUID = 1L;
  19. static final String PASS = "XXXXXXX";
  20. static final String SecurityToken = "XXXXXXXXXXXX";
  21. static final String USERNAME = "adminuser@.salesforce.com";
  22. static final String PASSWORD = PASS + SecurityToken;
  23. static final String LOGINURL = "https://login.salesforce.com";
  24. static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
  25. static final String CLIENTID = "ConsumerKeyFromSalesfoceConnectedApps";
  26. static final String CLIENTSECRET = "ConsumerSecretFromSalesforceConnectedApps";
  27.  
  28. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. HttpClient httpclient = HttpClientBuilder.create().build();
  30. String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET
  31. + "&username=" + USERNAME + "&password=" + PASSWORD;
  32.  
  33. HttpPost httpPost = new HttpPost(loginURL);
  34. HttpResponse resp = null;
  35.  
  36. try {
  37. resp = httpclient.execute(httpPost);
  38. } catch (ClientProtocolException cpException) {
  39. cpException.printStackTrace();
  40. } catch (IOException ioException) {
  41. ioException.printStackTrace();
  42. }
  43.  
  44. final int statusCode = resp.getStatusLine().getStatusCode();
  45. if (statusCode != HttpStatus.SC_OK) {
  46. System.out.println("Error authenticating to Force.com: " + statusCode);
  47. return;
  48. }
  49.  
  50. String getResult = null;
  51. try {
  52. getResult = EntityUtils.toString(resp.getEntity());
  53. } catch (IOException ioException) {
  54. ioException.printStackTrace();
  55. }
  56.  
  57. JSONObject jsonObject = null;
  58. String loginAccessToken = null;
  59. String loginInstanceUrl = null;
  60. try {
  61. jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
  62. loginAccessToken = jsonObject.getString("access_token");
  63. loginInstanceUrl = jsonObject.getString("instance_url");
  64. } catch (JSONException jsonException) {
  65. jsonException.printStackTrace();
  66. }
  67.  
  68. System.out.println(resp.getStatusLine());
  69. System.out.println("Successful login");
  70. System.out.println(" instance URL: " + loginInstanceUrl);
  71. System.out.println(" access token/session ID: " + loginAccessToken);
  72.  
  73. httpPost.releaseConnection();
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement