@Override public Map<String, String> get() { ListLabelsResponse response = null; try { response = getOrCreateGmail(authData).users().labels().list(USER).execute(); } catch (IOException e) { throw new RuntimeException("Unable to list labels for user", e); } ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); for (Label label : response.getLabels()) { // TODO: remove system labels builder.put(label.getName(), label.getId()); } return builder.build(); } };
@Before public void setUp() throws IOException { Label label = new Label(); label.setId(LABEL1); label.setName(LABEL1); labelsListResponse = new ListLabelsResponse().setLabels(Collections.singletonList(label)); jobStore = new LocalJobStore(); Monitor monitor = new Monitor() {}; googleMailImporter = new GoogleMailImporter(googleCredentialFactory, jobStore, gmail, monitor); when(gmail.users()).thenReturn(users); when(users.messages()).thenReturn(messages); when(messages.insert(Matchers.anyString(), Matchers.any(Message.class))).thenReturn(insert); when(users.labels()).thenReturn(labels); when(labels.list(Matchers.anyString())).thenReturn(labelsList); when(labelsList.execute()).thenReturn(labelsListResponse); when(labels.create(Matchers.anyString(), Matchers.any(Label.class))).thenReturn(labelsCreate); when(labelsCreate.execute()).thenReturn(label); verifyZeroInteractions(googleCredentialFactory); }
/** * Loads the user's labels and remembers them. */ void loadLabels() throws IOException { ListLabelsResponse labelResponse = gmailService.getServiceWithRetries() .users() .labels() .list(user.getEmailAddress()) .execute(); Verify.verify(!labelResponse.isEmpty(), "could not get labels %s"); List<Label> labels = labelResponse.getLabels(); labelsByName = labels.stream().collect(toMap(Label::getName, label -> label)); labelsById = labels.stream().collect(toMap(Label::getId, label -> label)); System.err.format("Got labels: %s", labelsByName); }
public static void main(String... args) throws IOException, GeneralSecurityException { // Build a new authorized API client service. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) .setApplicationName(APPLICATION_NAME) .build(); // Print the labels in the user's account. String user = "me"; ListLabelsResponse listResponse = service.users().labels().list(user).execute(); List<Label> labels = listResponse.getLabels(); if (labels.isEmpty()) { System.out.println("No labels found."); } else { System.out.println("Labels:"); for (Label label : labels) { System.out.printf("- %s\n", label.getName()); } } } }