Search code examples
javaspringsendgrid

Authorization Exception While implementing "/messages" endpoint in sendgrid API


I am developing a jsf + spring application and in that trying to get all the emails send through the sendgrid api using the "/messages" endpoint .

I tried implementing the "/messages" endpoint in Java similar to the "/stats" endpoint mentioned in the example here. Below is my code

I am initializing the send grid object in my application context

<bean id="sendGrid" class="com.sendgrid.SendGrid">
    <constructor-arg name="apiKey" value="my_api_key"/>
</bean>

Then I am using the autowired instance of that object in my service bean as follows:

com.sendgrid.Request request = new Request();
try {
   request.setMethod(Method.GET);
   request.setEndpoint("messages");
   request.addQueryParam("limit", "10");
   request.addQueryParam("query", "status='processed'");
   com.sendgrid.Response response=sendGrid.api(request);
   LOGGER.debug(response.getBody());
} catch (IOException e) {
   LOGGER.error(ExceptionUtils.getStackTrace(e));
}

However I am receiving the following exception

Stack Trace:

java.io.IOException: Request returned status Code 400 Body:{"errors":[{"message":"authorization required"}]}
at com.sendgrid.Client.executeApiCall(Client.java:287)
at com.sendgrid.Client.get(Client.java:163)
at com.sendgrid.Client.api(Client.java:308)
at com.sendgrid.SendGrid.makeCall(SendGrid.java:203)
at com.sendgrid.SendGrid.api(SendGrid.java:225)

Since I am able to send e-mail and receive stats successfully. I don't think authorization should be the problem here. Am I going wrong somewhere or am I missing something?

Any guesses or leads welcome. Thanks


Solution

  • I contacted the sendgrid support team and It was an API Key issue. Since I was trying out the free account, I did not have access to that particular endpoint. However once I upgraded to a paid account with the email activity add on, It worked.