Search in sources :

Example 1 with TokenResponseException

use of com.google.api.client.auth.oauth2.TokenResponseException in project Saber-Bot by notem.

the class OAuthCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    String message = "";
    if (args.length == 0) {
        String googleAuth;
        Credential credential;
        try {
            credential = GoogleAuth.authorize(event.getAuthor().getId());
            googleAuth = GoogleAuth.newAuthorizationUrl();
        } catch (IOException e) {
            return;
        }
        // send information to the user
        if (credential == null) {
            message = "There is no Google account associated with your Discord User ID.\n" + "Authorize with following link to receive an authorization token.\n" + "You can then use that token with the ``" + this.name() + "`` command to link your Google account.\n" + googleAuth;
        } else {
            message = "Your Discord User ID is currently associated with a Google Account access token.\n" + "You can authorize access to a different account's calendars by obtaining a different access token from the following link.\n" + "However, only one Google Account at a time may be linked with your Discord User ID.\n" + googleAuth;
        }
    } else {
        if (args[0].equalsIgnoreCase("off")) {
            try {
                GoogleAuth.unauthorize(event.getAuthor().getId());
                message = "Your associated authorization token as been removed.";
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                if (GoogleAuth.authorize(args[0], event.getAuthor().getId()) == null) {
                    message = "I failed to authorize your account!";
                } else {
                    message = "I've been successfully authorized by your Google Account!\n" + "Your Google authentication token has been linked to your Discord ID.";
                }
            } catch (TokenResponseException e) {
                Logging.exception(this.getClass(), e);
                message = "I failed to authorize your account! " + e.getDetails().getErrorDescription();
            } catch (IOException e) {
                message = "I failed to authorize your account!";
            }
        }
    }
    // send the message to the user
    if (event.isFromType(ChannelType.PRIVATE)) {
        MessageUtilities.sendPrivateMsg(message, event.getAuthor(), null);
    } else {
        MessageUtilities.sendMsg(message, event.getTextChannel(), null);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Example 2 with TokenResponseException

use of com.google.api.client.auth.oauth2.TokenResponseException in project cloudbreak by hortonworks.

the class GcpCredentialConnector method verify.

@Override
public CloudCredentialStatus verify(@Nonnull AuthenticatedContext authenticatedContext) {
    Objects.requireNonNull(authenticatedContext);
    LOGGER.info("Verify credential: {}", authenticatedContext.getCloudCredential());
    GcpContext gcpContext = gcpContextBuilder.contextInit(authenticatedContext.getCloudContext(), authenticatedContext, null, null, false);
    try {
        checkGcpContextValidity(gcpContext);
        preCheckOfGooglePermission(gcpContext);
    } catch (TokenResponseException te) {
        return createFailedCloudCredentialStatusWithExc(te, authenticatedContext, getErrDescriptionFromTokenResponse(te));
    } catch (Exception e) {
        return createFailedCloudCredentialStatusWithExc(e, authenticatedContext, Optional.empty());
    }
    return new CloudCredentialStatus(authenticatedContext.getCloudCredential(), CredentialStatus.VERIFIED);
}
Also used : GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) IOException(java.io.IOException) InvalidGcpContextException(com.sequenceiq.cloudbreak.cloud.gcp.context.InvalidGcpContextException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)

Aggregations

TokenResponseException (com.google.api.client.auth.oauth2.TokenResponseException)2 IOException (java.io.IOException)2 Credential (com.google.api.client.auth.oauth2.Credential)1 GcpContext (com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext)1 InvalidGcpContextException (com.sequenceiq.cloudbreak.cloud.gcp.context.InvalidGcpContextException)1 CloudCredentialStatus (com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)1