0
votes

I'm trying to make a GMAIL API call to the history endpoint using a historyId. This call works on Postman and I receive a json response as I should. But when I'm trying to implement the same in Android Studio, it returns a 404 Not Found Error. Can anyone help me with this?

URL urlHist = new URL("https://www.googleapis.com/gmail/v1/users/me/history?startHistoryId="+historyId);
            HttpURLConnection connHist = (HttpURLConnection) urlHist.openConnection();
            connHist.setRequestProperty("Authorization",token);
            connHist.setRequestMethod("GET");
            connHist.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            connHist.setRequestProperty("Accept","application/json");
            connHist.setDoOutput(true);
            connHist.setDoInput(true);

            DataOutputStream os = new DataOutputStream(connHist.getOutputStream());
            os.flush();
            os.close();
1

1 Answers

0
votes

You should use the Gmail API to build your requests. Here you can find an example on how to get started in Java. Your request should translate in something like this:

private static final String APPLICATION_NAME = "Your Android APP name";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

//...

final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

ListHistoryResponse history = service.users().history().list("me").setStartHistoryId(historyId).execute();