Search in sources :

Example 1 with CodeResponse

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);
    }
}
Also used : CodeResponse(com.cloudcraftgaming.discal.api.object.json.google.CodeResponse) Gson(com.google.gson.Gson) JsonNode(com.mashape.unirest.http.JsonNode) IOException(java.io.IOException) Type(java.lang.reflect.Type) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IUser(sx.blah.discord.handle.obj.IUser) Poll(com.cloudcraftgaming.discal.api.object.network.google.Poll)

Aggregations

CodeResponse (com.cloudcraftgaming.discal.api.object.json.google.CodeResponse)1 Poll (com.cloudcraftgaming.discal.api.object.network.google.Poll)1 Gson (com.google.gson.Gson)1 JsonNode (com.mashape.unirest.http.JsonNode)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 IUser (sx.blah.discord.handle.obj.IUser)1 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)1