Pfrances
Pfrances

Reputation: 127

SendGrid: "The provided authorization grant is invalid, expired, or revoked"

I am setting up an app to send emails to clients. I am using Java and SendGrid to create this app, but I am having issues with the SendGrid authorization

I am getting the error:

java.io.IOException: Request returned status Code 401Body:{"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}

Reading through some other posts with similar issues it seems most get resolved because people were using the API Key ID instead of the API Key. I have already checked to make sure that I am using the full key so I don't think that is the issue.

I have also tried to create a new API Key but that just gives the same issue. Finally, I have tried doing a similar call using Postman and that works just fine with the same API Keys.

This is the main class I am using to send emails.


import com.sendgrid.*;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;

import java.io.IOException;

public class SendEmail {

    public static void main(String[] args) throws IOException{
        Email from = new Email("FROM_EMAIL");
        String subject = "Sending with Twilio(?) is fun";
        Email to = new Email("TO_EMAIL");
        Content content = new Content("text/plain", "and easy to to anywhere, even with Java");
        Mail mail = new Mail(from, subject, to, content);

        SendGrid sg = new SendGrid(System.getenv("API_KEY"));
        Request request = new Request();

        try {
            request.setMethod(Method.POST);
            request.setEndpoint("mail/send");
            request.setBody(mail.build());
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
        } catch (IOException ex) {
            throw(ex);
        }

    }

I then call this class in my main class through this snippet:


    try{
            SendEmail.main(email);
    } catch(IOException ex){
            System.out.println(ex);
    }

The "email" string is just a placeholder as otherwise I get an error saying that I need to have an array when I call the class. (Not sure why that is, could that be the root of the problem?)

Obviously sensitive information has been removed so that's why some of the fields look off.

By the way, I followed the tutorial in this github:

https://github.com/sendgrid/sendgrid-java

Let me know what you think. Thanks in advance!

Upvotes: 2

Views: 4978

Answers (2)

Yashith Chanuka
Yashith Chanuka

Reputation: 59

I think your environment variable was not setup properly. Try to execute the program without "System.getenv" as below.

SendGrid sg = new SendGrid("YOUR_API_KEY");

Upvotes: 1

Pfrances
Pfrances

Reputation: 127

My apologies. I did not know "System.getenv" would get the environmental variables. I set up environmental variables and the issue is resolved.

Upvotes: 0

Related Questions