Users.labels: list

Requires authorization

Lists all labels in the user's mailbox. Try it now or see an example.

Request

HTTP request

GET https://www.googleapis.com/gmail/v1/users/userId/labels

Parameters

Parameter name Value Description
Path parameters
userId string The user's email address. The special value me can be used to indicate the authenticated user.

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.labels
https://www.googleapis.com/auth/gmail.metadata

Request body

Do not supply a request body with this method.

Response

If successful, this method returns a response body with the following structure:

{
  "labels": [
    users.labels Resource
  ]
}
Property name Value Description Notes
labels[] list List of labels.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Label;
import com.google.api.services.gmail.model.ListLabelsResponse;

import java.io.IOException;
import java.util.List;

// ...

public class MyClass {

  // ...

  /**
   * List the Labels in the user's mailbox.
   *
   * @param service Authorized Gmail API instance.
   * @param userId User's email address. The special value "me"
   * can be used to indicate the authenticated user.
   * @throws IOException
   */
  public static void listLabels(Gmail service, String userId) throws IOException {
    ListLabelsResponse response = service.users().labels().list(userId).execute();
    List<Label> labels = response.getLabels();
    for (Label label : labels) {
      System.out.println(label.toPrettyString());
    }
  }

  // ...

}

.NET

Uses the .NET client library.

using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// List the labels in the user's mailbox.
  /// </summary>
  /// <param name="service">Gmail API service instance.</param>
  /// <param name="userId">User's email address. The special value "me"
  /// can be used to indicate the authenticated user.</param>
  public static void ListLabels(GmailService service, String userId)
  {
    try
    {
      ListLabelsResponse response = service.Users.Labels.List(userId).Execute();
      foreach (Label label in response.Labels)
      {
        Console.WriteLine(label.Id + " - " + label.Name);
      }
    }
    catch (Exception e)
    {
      Console.WriteLine("An error occurred: " + e.Message);
    }
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Get all the Labels in the user's mailbox.
 *
 * @param  Google_Service_Gmail $service Authorized Gmail API instance.
 * @param  string $userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @return array Array of Labels.
 */
function listLabels($service, $userId) {
  $labels = array();

  try {
    $labelsResponse = $service->users_labels->listUsersLabels($userId);

    if ($labelsResponse->getLabels()) {
      $labels = array_merge($labels, $labelsResponse->getLabels());
    }

    foreach ($labels as $label) {
      print 'Label with ID: ' . $label->getId() . '<br/>';
    }
  } catch (Excetion $e) {
    print 'An error occurred: ' . $e->getMessage();
  }

  return $labels;
}

Python

Uses the Python client library.

"""Get a list of Labels from the user's mailbox.
"""

from apiclient import errors


def ListLabels(service, user_id):
  """Get a list all labels in the user's mailbox.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.

  Returns:
    A list all Labels in the user's mailbox.
  """
  try:
    response = service.users().labels().list(userId=user_id).execute()
    labels = response['labels']
    for label in labels:
      print 'Label id: %s - Label name: %s' % (label['id'], label['name'])
    return labels
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

JavaScript

Does not use a client library.

/**
 * Get all the Labels in the user's mailbox.
 *
 * @param  {String} userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  {Function} callback Function to call when the request is complete.
 */
function listLabels(userId, callback) {
  var request = gapi.client.gmail.users.labels.list({
    'userId': userId
  });
  request.execute(function(resp) {
    var labels = resp.labels;
    callback(labels);
  });
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response.

Enviar comentarios sobre…

¿Necesitas ayuda? Visita nuestra página de asistencia.