Users.threads: get

Requires authorization

Gets the specified thread. Try it now or see an example.

Request

HTTP request

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

Parameters

Parameter name Value Description
Path parameters
id string The ID of the thread to retrieve.
userId string The user's email address. The special value me can be used to indicate the authenticated user.
Optional query parameters
format string The format to return the messages in.

Acceptable values are:
  • "full": Returns the parsed email message content in the payload field and the raw field is not used. (default)
  • "metadata": Returns email headers with message metadata such as identifiers and labels.
  • "minimal": Only returns email message metadata such as identifiers and labels, it does not return the email headers, body, or payload.
metadataHeaders[] list When given and format is METADATA, only include headers specified.

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.metadata

Request body

Do not supply a request body with this method.

Response

If successful, this method returns a Users.threads resource in the response body.

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.Thread;

import java.io.IOException;

// ...

public class MyClass {

  // ...

  /**
   * Get a Thread with given id.
   *
   * @param service Authorized Gmail API instance.
   * @param userId User's email address. The special value "me"
   * can be used to indicate the authenticated user.
   * @param threadId ID of Thread to retrieve.
   * @throws IOException
   */
  public static void getThread(Gmail service, String userId, String threadId) throws IOException {
    Thread thread = service.users().threads().get(userId, threadId).execute();
    System.out.println("Thread id: " + thread.getId());
    System.out.println("No. of messages in this thread: " + thread.getMessages().size());
    System.out.println(thread.toPrettyString());
  }

  // ...

}

.NET

Uses the .NET client library.

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

// ...

public class MyClass {

  // ...

  /// <summary>
  /// Get a Thread with given ID.
  /// </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>
  /// <param name="threadId">ID of Thread to retrieve.</param>
  public static Thread GetThread(GmailService service, String userId, String threadId)
  {
      try
      {
          return service.Users.Threads.Get(userId, threadId).Execute();
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }

      return null;
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Get Thread with given ID.
 *
 * @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.
 * @param  string $threadId ID of Thread to get.
 * @return Google_Service_Gmail_Thread Retrieved Thread.
 */
function getThread($service, $userId, $threadId) {
  try {
    $thread = $service->users_threads->get($userId, $threadId);
    $messages = $thread->getMessages();
    $msgCount = count($messages);
    print 'Number of Messages in the Thread: ' . $msgCount;
    return $thread;
  } catch (Exception $e){
    print 'An error occurred: ' . $e->getMessage();
  }
}

Python

Uses the Python client library.

"""Get a Thread.
"""

from apiclient import errors


def GetThread(service, user_id, thread_id):
  """Get a Thread.

  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.
    thread_id: The ID of the Thread required.

  Returns:
    Thread with matching ID.
  """
  try:
    thread = service.users().threads().get(userId=user_id, id=thread_id).execute()
    messages = thread['messages']
    print ('thread id: %s - number of messages '
           'in this thread: %d') % (thread['id'], len(messages))
    return thread
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

JavaScript

Does not use a client library.

/**
 * Get Thread with given ID.
 *
 * @param  {String} userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  {String} threadId ID of Thread to get.
 * @param  {Function} callback Function to call when the request is complete.
 */
function getThread(userId, threadId, callback) {
  var request = gapi.client.gmail.users.threads.get({
    'userId': userId,
    'id': threadId
  });
  request.execute(callback);
}

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.