Skip to content

Instantly share code, notes, and snippets.

@jderda
Created October 19, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jderda/ec4ec4349e20ea8ad91e85398c3839dd to your computer and use it in GitHub Desktop.
Save jderda/ec4ec4349e20ea8ad91e85398c3839dd to your computer and use it in GitHub Desktop.
Sample HttpClient code using OAuth token
public void readRemoteData() throws URISyntaxException {
String oauthToken = "ABCDEFGHIJK....";
CloseableHttpClient httpclient = HttpClients.createDefault();
URIBuilder requestUri = new URIBuilder("https://query.yahooapis.com/v1/public/yql");
requestUri.addParameter("format", "json");
// tutaj kolejne parametry związane z zapytaniem...
HttpGet httpGet = new HttpGet(requestUri.build());
httpGet.addHeader("Authorization", "Bearer " + oauthToken);
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
entity.getContent();
//tutaj przetwarzanie odpowiedzi...
EntityUtils.consume(entity);
} catch (IOException | UnsupportedOperationException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment