@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(); } };
@Override public void onSuccess( Label label, HttpHeaders responseHeaders) throws IOException { labelIdToNameMap.put(label.getId(), label.getName()); } });
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()); } } } }
/** * Creates a new label. * * Create a request for the method "labels.create". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Create#execute()} method to invoke the remote operation. <p> {@link * Create#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must * be called to initialize this instance immediately after invoking the constructor. </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param content the {@link com.google.api.services.gmail.model.Label} * @since 1.13 */ protected Create(java.lang.String userId, com.google.api.services.gmail.model.Label content) { super(Gmail.this, "POST", REST_PATH, content, com.google.api.services.gmail.model.Label.class); this.userId = com.google.api.client.util.Preconditions.checkNotNull(userId, "Required parameter userId must be specified."); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getLabelListVisibility(), "Label.getLabelListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getMessageListVisibility(), "Label.getMessageListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getName(), "Label.getName()"); }
/** * Updates the specified label. * * Create a request for the method "labels.update". * * This request holds the parameters needed by the the gmail server. After setting any optional * parameters, call the {@link Update#execute()} method to invoke the remote operation. <p> {@link * Update#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must * be called to initialize this instance immediately after invoking the constructor. </p> * * @param userId The user's email address. The special value me can be used to indicate the authenticated user. * [default: me] * @param id The ID of the label to update. * @param content the {@link com.google.api.services.gmail.model.Label} * @since 1.13 */ protected Update(java.lang.String userId, java.lang.String id, com.google.api.services.gmail.model.Label content) { super(Gmail.this, "PUT", REST_PATH, content, com.google.api.services.gmail.model.Label.class); this.userId = com.google.api.client.util.Preconditions.checkNotNull(userId, "Required parameter userId must be specified."); this.id = com.google.api.client.util.Preconditions.checkNotNull(id, "Required parameter id must be specified."); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getId(), "Label.getId()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getLabelListVisibility(), "Label.getLabelListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getMessageListVisibility(), "Label.getMessageListVisibility()"); checkRequiredParameter(content, "content"); checkRequiredParameter(content.getName(), "Label.getName()"); }