Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. public class Main {
  2.  
  3. static final String USERNAME = "tanujsfdcfsp@gmail.com";
  4. static final String PASSWORD = "******************";
  5. static final String LOGINURL = "https://login.salesforce.com";
  6. static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
  7. static final String CLIENTID = "***********";
  8. static final String CLIENTSECRET = "********************";
  9.  
  10. public static void main(String[] args) {
  11.  
  12. DefaultHttpClient httpclient = new DefaultHttpClient();
  13.  
  14. // Assemble the login request URL
  15. String loginURL = LOGINURL +
  16. GRANTSERVICE +
  17. "&client_id=" + CLIENTID +
  18. "&client_secret=" + CLIENTSECRET +
  19. "&username=" + USERNAME +
  20. "&password=" + PASSWORD;
  21.  
  22. // Login requests must be POSTs
  23. HttpPost httpPost = new HttpPost(loginURL);
  24. /// HttpGet httpGet = new HttpGet(loginURL);
  25. HttpResponse response = null;
  26.  
  27. try {
  28. // Execute the login POST request
  29. response = httpclient.execute(httpPost);
  30. } catch (ClientProtocolException cpException) {
  31. System.out.println("Exception: "+cpException);
  32. // Handle protocol exception
  33. } catch (IOException ioException) {
  34. // Handle system IO exception
  35. }
  36.  
  37. // verify response is HTTP OK
  38. final int statusCode = response.getStatusLine().getStatusCode();
  39. if (statusCode != HttpStatus.SC_OK) {
  40. System.out.println("Error authenticating to Force.com: "+statusCode);
  41. // Error is in EntityUtils.toString(response.getEntity())
  42. return;
  43. }
  44.  
  45. String getResult = null;
  46. try {
  47. getResult = EntityUtils.toString(response.getEntity());
  48. } catch (IOException ioException) {
  49. // Handle system IO exception
  50. }
  51. JSONObject jsonObject = null;
  52. String loginAccessToken = null;
  53. String loginInstanceUrl = null;
  54. try {
  55. jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
  56. loginAccessToken = jsonObject.getString("access_token");
  57. loginInstanceUrl = jsonObject.getString("instance_url");
  58. } catch (JSONException jsonException) {
  59. // Handle JSON exception
  60. }
  61. System.out.println(response.getStatusLine());
  62. System.out.println("Successful login");
  63. System.out.println(" instance URL: "+loginInstanceUrl);
  64. System.out.println(" access token/session ID: "+loginAccessToken);
  65.  
  66. // release connection
  67. httpPost.releaseConnection();
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement