use of com.cloudcraftgaming.discal.api.object.json.google.AuthRefreshResponse in project DisCal-Discord-Bot by NovaFox161.
the class Authorization method requestNewAccessToken.
public String requestNewAccessToken(GuildSettings settings, AESEncryption encryption) {
try {
String body = "client_id=" + clientData.getClientId() + "&client_secret=" + clientData.getClientSecret() + "&refresh_token=" + encryption.decrypt(settings.getEncryptedRefreshToken()) + "&grant_type=refresh_token";
com.mashape.unirest.http.HttpResponse<JsonNode> httpResponse = Unirest.post("https://www.googleapis.com/oauth2/v4/token").header("Content-Type", "application/x-www-form-urlencoded").body(body).asJson();
if (httpResponse.getStatus() == HttpStatusCodes.STATUS_CODE_OK) {
Type type = new TypeToken<AuthRefreshResponse>() {
}.getType();
AuthRefreshResponse response = new Gson().fromJson(httpResponse.getBody().toString(), type);
// Update Db data.
settings.setEncryptedAccessToken(encryption.encrypt(response.access_token));
DatabaseManager.getManager().updateSettings(settings);
// Okay, we can return the access token to be used when this method is called.
return response.access_token;
} else {
// Failed to get OK. Send debug info.
Logger.getLogger().debug(null, "Error requesting new access token.", "Status code: " + httpResponse.getStatus() + " | " + httpResponse.getStatusText() + " | " + httpResponse.getBody().toString(), this.getClass(), true);
return null;
}
} catch (Exception e) {
// Error occurred, lets just log it and return null.
Logger.getLogger().exception(null, "Failed to request new access token.", e, this.getClass(), true);
return null;
}
}
Aggregations