How do I get the content of all emails from a user in the Gmail API?

Asked

Viewed 692 times

1

I’m making an application that should receive Gmail user emails using the Gmail API. However, I must receive the data of each email as: content, sender, subject and date received, but I can not do.

Follow the excerpt I picked up in Google where I can only get the ID and Thread ID of each email:

public class GmailApiQuickstart {

// Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
private static final String SCOPE = "https://www.googleapis.com/auth/gmail.readonly";
private static final String APP_NAME = "GmailApiQuickstart";
private static final String USER = "me";
// Path to the client_secret.json file downloaded from the Developer Console
private static final String CLIENT_SECRET_PATH = "c:/client_secret.json";

private static GoogleClientSecrets clientSecrets;

public void buscar() throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();

clientSecrets = GoogleClientSecrets.load(jsonFactory,  new FileReader(CLIENT_SECRET_PATH));

// Allow user to authorize via url.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
    .setAccessType("online")
    .setApprovalPrompt("auto").build();

String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
    .build();
System.out.println("Please open the following URL in your browser then type"
                   + " the authorization code:\n" + url);

// Read code entered by user.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();

// Generate Credential using retrieved code.
GoogleTokenResponse responseT = flow.newTokenRequest(code)
    .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential()
    .setFromTokenResponse(responseT);

// Create a new authorized Gmail API client
Gmail service = new Gmail.Builder(httpTransport, jsonFactory, credential)
    .setApplicationName(APP_NAME).build();

ListMessagesResponse response = service.users().messages().list(USER).setQ("vagas").execute();

List<Message> messages = new ArrayList<Message>();
while (response.getMessages() != null) {
  messages.addAll(response.getMessages());
  if (response.getNextPageToken() != null) {
    String pageToken = response.getNextPageToken();
    response = service.users().messages().list(USER).setQ("vagas")
        .setPageToken(pageToken).execute();
  } else {
    break;
  }
}

for (Message message : messages) {
    //Essa é a parte onde pega o ID e Thread ID
    //qual(ais) parâmetro(s) devo adicionar para pegar o que preciso???
    System.out.println(message.toPrettyString());
}
}

}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.