use of com.cloudcraftgaming.discal.api.object.json.google.CodeResponse in project DisCal-Discord-Bot by NovaFox161.
the class Authorization method requestCode.
public void requestCode(MessageReceivedEvent event, GuildSettings settings) {
try {
String body = "client_id=" + clientData.getClientId() + "&scope=" + CalendarScopes.CALENDAR;
com.mashape.unirest.http.HttpResponse<JsonNode> response = Unirest.post("https://accounts.google.com/o/oauth2/device/code").header("Content-Type", "application/x-www-form-urlencoded").body(body).asJson();
if (response.getStatus() == HttpStatusCodes.STATUS_CODE_OK) {
Type type = new TypeToken<CodeResponse>() {
}.getType();
CodeResponse cr = new Gson().fromJson(response.getBody().toString(), type);
// Send DM to user with code.
EmbedBuilder em = new EmbedBuilder();
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
em.withAuthorName("DisCal");
em.withTitle(MessageManager.getMessage("Embed.AddCalendar.Code.Title", settings));
em.appendField(MessageManager.getMessage("Embed.AddCalendar.Code.Code", settings), cr.user_code, true);
em.withFooterText(MessageManager.getMessage("Embed.AddCalendar.Code.Footer", settings));
em.withUrl(cr.verification_url);
em.withColor(36, 153, 153);
IUser user = event.getAuthor();
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Success", settings), em.build(), user);
// Start timer to poll Google Cal for auth
Poll poll = new Poll(user, event.getGuild());
poll.setDevice_code(cr.device_code);
poll.setRemainingSeconds(cr.expires_in);
poll.setExpires_in(cr.expires_in);
poll.setInterval(cr.interval);
pollForAuth(poll);
} else {
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Failure.NotOkay", settings), event.getAuthor());
Logger.getLogger().debug(event.getAuthor(), "Error requesting access token.", "Status code: " + response.getStatus() + " | " + response.getStatusText() + " | " + response.getBody().toString(), this.getClass(), true);
}
} catch (Exception e) {
// Failed, report issue to dev.
Logger.getLogger().exception(event.getAuthor(), "Failed to request Google Access Code", e, this.getClass(), true);
IUser u = event.getAuthor();
Message.sendDirectMessage(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Failure.Unknown", settings), u);
}
}
Aggregations