Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static final String LOGINURL = "https://login.salesforce.com";
- static final String GRANTSERVICE = "/services/oauth2/tokengrant_type=password";
- private static final String CLIENTID = "*******";
- private static final String CLIENTSECRET = "----25284058416";
- private static final String USERID = "****userID.com";
- private static final String PASSWORD = "*****"; //password + securityToken
- private static final String ACCESSTOKEN = "access_token";
- private static final String INSTANCEURL = "instance_url";
- private static String instanceUrl;
- private static Header oAuthHeader;
- public static void main(String[] args) {
- HttpClient httpclient = HttpClientBuilder.create().build();
- String loginURL = LOGINURL +
- GRANTSERVICE +
- "&client_id=" + CLIENTID +
- "&client_secret=" + CLIENTSECRET +
- "&username=" + USERID +
- "&password=" + PASSWORD;
- HttpPost httpPost = new HttpPost(loginURL);
- HttpResponse httpResponse = null;
- try {
- httpResponse = httpclient.execute(httpPost);
- } catch (ClientProtocolException clientProtocolException) {
- clientProtocolException.printStackTrace();
- } catch (IOException ioException) {
- ioException.printStackTrace();
- } catch (Exception exception) {
- exception.printStackTrace();
- }
- final int statusCode = httpResponse.getStatusLine().getStatusCode();
- if (statusCode != HttpStatus.SC_OK) {
- System.out.println("Error: " + statusCode + "tt "+ httpResponse.getStatusLine().getReasonPhrase());
- System.out.println("Error authenticating to Salesforce.com platform: " + statusCode);
- return;
- }
- String httpMessage = null;
- try {
- httpMessage = EntityUtils.toString(httpResponse.getEntity());
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- JSONObject jsonObject = null;
- String accessToken = null;
- try {
- jsonObject = (JSONObject) new JSONTokener(httpMessage).nextValue();
- accessToken = jsonObject.getString(ACCESSTOKEN);
- instanceUrl = jsonObject.getString(INSTANCEURL);
- } catch (JSONException jsonException) {
- jsonException.printStackTrace();
- }
- //oAuthHeader = new BasicHeader("Authorization", "OAuth2 " + accessToken);
- httpPost.releaseConnection();
- }
Add Comment
Please, Sign In to add comment