Advertisement
Vaerys_Dawn

Patreon Refresh

Jan 12th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. public static void refreshPatreonToken(String clientID, String clientSecret, String refreshToken, List<String> contents) {
  2.         try {
  3.             String refreshURL = "http://www.patreon.com/api/oauth2/";
  4.             String urlArgs = String.format("token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s", refreshToken, clientID, clientSecret);
  5.             URL url = new URL(refreshURL);
  6.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  7.             connection.setRequestMethod("POST");
  8.             connection.setRequestProperty("User-Agent", "Mozilla/5.0");
  9.             connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  10.             connection.setDoOutput(true);
  11.             connection.connect();
  12.             DataOutputStream write = new DataOutputStream(connection.getOutputStream());
  13.             write.writeBytes(urlArgs);
  14.             write.flush();
  15.             write.close();
  16.  
  17.             logger.info("Sending request to refresh token.");
  18.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  19.                 InputStream stream = connection.getInputStream();
  20.                 Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
  21.                 JsonParser parser = new JsonParser();
  22.                 JsonElement jsonElement = parser.parse(reader);
  23.                 reader.close();
  24.                 stream.close();
  25.                 Gson gson = new Gson();
  26.                 TokenRefreshObject object = gson.fromJson(jsonElement, TokenRefreshObject.class);
  27.                 if (object != null) {
  28.                     logger.info("Token Refreshed");
  29.                     contents.set(0, object.getAccessToken());
  30.                 }
  31.             }
  32.         } catch (MalformedURLException e) {
  33.             e.printStackTrace();
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement