Guest User

Untitled

a guest
Feb 2nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. static final String LOGINURL = "https://login.salesforce.com";
  2. static final String GRANTSERVICE = "/services/oauth2/tokengrant_type=password";
  3. private static final String CLIENTID = "*******";
  4. private static final String CLIENTSECRET = "----25284058416";
  5. private static final String USERID = "****userID.com";
  6. private static final String PASSWORD = "*****"; //password + securityToken
  7.  
  8. private static final String ACCESSTOKEN = "access_token";
  9. private static final String INSTANCEURL = "instance_url";
  10. private static String instanceUrl;
  11. private static Header oAuthHeader;
  12.  
  13. public static void main(String[] args) {
  14. HttpClient httpclient = HttpClientBuilder.create().build();
  15.  
  16. String loginURL = LOGINURL +
  17. GRANTSERVICE +
  18. "&client_id=" + CLIENTID +
  19. "&client_secret=" + CLIENTSECRET +
  20. "&username=" + USERID +
  21. "&password=" + PASSWORD;
  22.  
  23. HttpPost httpPost = new HttpPost(loginURL);
  24. HttpResponse httpResponse = null;
  25.  
  26. try {
  27. httpResponse = httpclient.execute(httpPost);
  28. } catch (ClientProtocolException clientProtocolException) {
  29. clientProtocolException.printStackTrace();
  30. } catch (IOException ioException) {
  31. ioException.printStackTrace();
  32. } catch (Exception exception) {
  33. exception.printStackTrace();
  34. }
  35.  
  36. final int statusCode = httpResponse.getStatusLine().getStatusCode();
  37. if (statusCode != HttpStatus.SC_OK) {
  38. System.out.println("Error: " + statusCode + "tt "+ httpResponse.getStatusLine().getReasonPhrase());
  39. System.out.println("Error authenticating to Salesforce.com platform: " + statusCode);
  40. return;
  41. }
  42.  
  43. String httpMessage = null;
  44. try {
  45. httpMessage = EntityUtils.toString(httpResponse.getEntity());
  46. } catch (IOException ioException) {
  47. ioException.printStackTrace();
  48. }
  49.  
  50. JSONObject jsonObject = null;
  51. String accessToken = null;
  52. try {
  53. jsonObject = (JSONObject) new JSONTokener(httpMessage).nextValue();
  54. accessToken = jsonObject.getString(ACCESSTOKEN);
  55. instanceUrl = jsonObject.getString(INSTANCEURL);
  56. } catch (JSONException jsonException) {
  57. jsonException.printStackTrace();
  58. }
  59.  
  60. //oAuthHeader = new BasicHeader("Authorization", "OAuth2 " + accessToken);
  61. httpPost.releaseConnection();
  62. }
Add Comment
Please, Sign In to add comment