private String getFreshAccessToken() { LOGGER.info("About get SalesForce access token"); WebResource resource = client.resource(tokenEndPointUrl); Form form = new Form(); form.add("grant_type", "password"); form.add("client_id", clientId); form.add("client_secret", clientSecret); form.add("username", username); form.add("password", password); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON_TYPE).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, form); if (response.getStatus() == 200) { try { return response.getEntity(JSONObject.class).getString("access_token"); } catch (ClientHandlerException | UniformInterfaceException | JSONException e) { throw new RuntimeException("Unable to extract access token from response", e); } } else { throw new RuntimeException("Error getting access token from SalesForce, status code = " + response.getStatus() + ", reason = " + response.getStatusInfo().getReasonPhrase() + ", body = " + response.getEntity(String.class)); } }
/** * Ping as a POST * @param text text to include * @return the response * @throws IOException on any failure */ public PingInformation pingPost(String text) throws IOException { WebResource pingResource = applicationResource(ACTION_PING); Form f = new Form(); f.add("text", text); return pingResource .type(MediaType.APPLICATION_JSON_TYPE) .post(PingInformation.class, f); }
/** * Ping as a POST * @param text text to include * @return the response * @throws IOException on any failure */ public PingInformation pingPost(String text) throws IOException { WebResource pingResource = applicationResource(ACTION_PING); Form f = new Form(); f.add("text", text); return pingResource .type(MediaType.APPLICATION_JSON_TYPE) .post(PingInformation.class, f); }
/** * {@inheritDoc} */ @Override public <T> T post(Map<String, String> formParams, Class<T> t) { AhcHttpClient asyncClient = AhcHttpClient.create(createAhcConfig()); try { Form form = new Form(); for (Map.Entry<String, String> e : formParams.entrySet()) { form.add(e.getKey(), e.getValue()); } WebResource r = buildRequest(asyncClient); return headers(r, TYPE.POST, true).post(t, form); } catch (UniformInterfaceException u) { headers.put(negotiateHandler.challengedHeaderName(), negotiate(u)); return post(formParams, t); } finally { asyncClient.destroy(); } }