Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. package src;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.http.Header;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.message.BasicHeader;
  12. import org.apache.http.util.EntityUtils;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15. import org.json.JSONTokener;
  16.  
  17. class Main{
  18. static final String USERNAME = "************";
  19. static final String PASSWORD = "pass+token";
  20. static final String LOGINURL = "https://login.salesforce.com";
  21. static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password&access_token=offline";
  22. static final String CLIENTID = "3MVG9g9rbsTkKnAVl5uH7qGQy3Cy8zeGcmScjViW4xVDWzMNHHn4LwAVz9OgqVvc5LTV574JRPfQtaGLbAaja";
  23. static final String CLIENTSECRET = "1179135639053529602";
  24. private static String REST_ENDPOINT = "/services/data" ;
  25. private static String API_VERSION = "/v41.0" ;
  26. private static String baseUri;
  27. private static Header oauthHeader;
  28. private static Header prettyPrintHeader = new BasicHeader("X-PrettyPrint", "1");
  29.  
  30. static String Name="basic.txt";
  31. static String AttachmentId="";
  32.  
  33.  
  34. public static void main(String[] args) {
  35.  
  36. @SuppressWarnings("deprecation")
  37. DefaultHttpClient httpclient = new DefaultHttpClient();
  38.  
  39. // Assemble the login request URL
  40.  
  41. String loginURL = LOGINURL +
  42.  
  43. GRANTSERVICE +
  44.  
  45. "&client_id=" + CLIENTID +
  46.  
  47. "&client_secret=" + CLIENTSECRET +
  48.  
  49. "&username=" + USERNAME +
  50.  
  51. "&password=" + PASSWORD;
  52.  
  53. // Login requests must be POSTs
  54.  
  55. System.out.println("login URL:"+loginURL);
  56.  
  57. HttpPost httpPost = new HttpPost(loginURL);
  58.  
  59. HttpResponse response = null;
  60.  
  61. try {
  62.  
  63. // Execute the login POST request
  64.  
  65. response = httpclient.execute(httpPost);
  66.  
  67. } catch (ClientProtocolException cpException) {
  68.  
  69. System.out.println("in client protocol exception:"+cpException);
  70.  
  71. // Handle protocol exception
  72.  
  73. } catch (IOException ioException) {
  74.  
  75. System.out.println("in ioexception:"+ioException);
  76.  
  77. // Handle system IO exception
  78.  
  79. }
  80.  
  81. //Setting Up Your Java Developer Environment Verify the REST Environment (REST-Based APIs)
  82.  
  83. // verify response is HTTP OK
  84.  
  85. final int statusCode =
  86.  
  87. response.getStatusLine().getStatusCode();
  88.  
  89. if (statusCode != HttpStatus.SC_OK) {
  90.  
  91. System.out.println("Error authenticating to Force.com:"+statusCode);
  92.  
  93. // Error is in EntityUtils.toString(response.getEntity())
  94.  
  95. return; }
  96.  
  97. String getResult = null;
  98.  
  99. try {
  100.  
  101. getResult = EntityUtils.toString(response.getEntity());
  102.  
  103. } catch (IOException ioException) {
  104.  
  105. // Handle system IO exception
  106.  
  107. }
  108.  
  109. JSONObject jsonObject = null;
  110.  
  111. String loginAccessToken = null;
  112.  
  113. String loginInstanceUrl = null;
  114.  
  115. try {
  116.  
  117. jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
  118.  
  119. loginAccessToken = jsonObject.getString("access_token");
  120.  
  121. loginInstanceUrl = jsonObject.getString("instance_url");
  122.  
  123. } catch (JSONException jsonException) {
  124.  
  125. // Handle JSON exception
  126.  
  127. }
  128.  
  129. System.out.println(response.getStatusLine());
  130.  
  131. System.out.println("Successful login");
  132.  
  133. System.out.println(" instance URL: "+loginInstanceUrl);
  134.  
  135. System.out.println(" access token/session ID:"+loginAccessToken);
  136.  
  137. // release connection
  138.  
  139. httpPost.releaseConnection();
  140.  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement