diff options
Diffstat (limited to 'src/io/BearerRequester.java')
-rw-r--r-- | src/io/BearerRequester.java | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/io/BearerRequester.java b/src/io/BearerRequester.java index 4cb554b..442f62a 100644 --- a/src/io/BearerRequester.java +++ b/src/io/BearerRequester.java @@ -45,27 +45,31 @@ public class BearerRequester extends AbstractRequester { String postData = "grant_type=client_credentials"; URL url = buildUrl("oauth2/token"); HttpURLConnection conn = open(url); - conn.setRequestMethod("POST"); - // set request headers - conn.addRequestProperty("Authorization", "Basic " - + secrets.getBearerTokenBase64()); - conn.addRequestProperty("Content-Type", - "application/x-www-form-urlencoded; charset=UTF-8"); - conn.setFixedLengthStreamingMode(postData.length()); - // connect and send request - conn.setDoOutput(true); - conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8)); - try { - JSONObject resp = getResponseAsJson(conn); - // TODO: parse resp.errors - if (!resp.getString("token_type").equals("bearer")) { - throw new IOException("Expected bearer token type"); + conn.setRequestMethod("POST"); + // set request headers + conn.addRequestProperty("Authorization", "Basic " + + secrets.getBearerTokenBase64()); + conn.addRequestProperty("Content-Type", + "application/x-www-form-urlencoded; charset=UTF-8"); + conn.setFixedLengthStreamingMode(postData.length()); + // connect and send request + conn.setDoOutput(true); + conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8)); + + try { + JSONObject resp = getResponseAsJson(conn); + // TODO: parse resp.errors + if (!resp.getString("token_type").equals("bearer")) { + throw new IOException("Expected bearer token type"); + } + access_token = resp.getString("access_token"); + } catch (JSONException ex) { + // treat JSON errors as if an I/O error occurred + throw new IOException(ex); } - access_token = resp.getString("access_token"); - } catch (JSONException ex) { - // treat JSON errors as if an I/O error occurred - throw new IOException(ex); + } finally { + conn.disconnect(); } } |