Users.drafts: get

Requires authorization

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

Request

HTTP request

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

Parameters

Parameter name Value Description
Path parameters
id string The ID of the draft 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 draft in.

Acceptable values are:
  • "full": Returns the full email message data with body content parsed in the payload field; the raw field is not used. (default)
  • "metadata": Returns only email message ID, labels, and email headers.
  • "minimal": Returns only email message ID and labels; does not return the email headers, body, or payload.
  • "raw": Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used.

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.compose
https://www.googleapis.com/auth/gmail.readonly

Request body

Do not supply a request body with this method.

Response

If successful, this method returns a Users.drafts 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.Draft;
import com.google.api.services.gmail.model.Message;

import java.io.IOException;

// ...

public class MyClass {

  // ...

  /**
   * Get Draft with specified Draft 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 draftId The ID of the Draft to return.
   * @return Draft with matching Draft ID.
   * @throws IOException
   */
  public static Draft getDraft(Gmail service, String userId, String draftId)
      throws IOException {
    Draft draft = service.users().drafts().get(userId, draftId).execute();
    Message message = draft.getMessage();

    System.out.println("Draft id: " + draft.getId() + "\nDraft Message:\n"
        + message.toPrettyString());

    return draft;
  }

  // ...

}

.NET

Uses the .NET client library.

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

// ...

public class MyClass {

  // ...

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

      return null;
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Get Draft 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 $draftId ID of Draft to get.
 * @return Google_Service_Gmail_Draft Created Draft.
 */
function getDraft($service, $user, $draftId) {
  try {
    $draft = $service->users_drafts->get($user, $draftId);
    $message = $draft->getMessage();

    print 'Draft with ID: ' . $draft->getId() . ' contains Message with ID: ' . $message->getId();

    return $draft;
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }
}

Python

Uses the Python client library.

"""Get Draft with specified Draft ID.
"""

from apiclient import errors


def GetDraft(service, user_id, draft_id):
  """Get Draft with ID matching draft_id.

  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.
    draft_id: The ID of the Draft to return.

  Returns:
    Draft with ID matching draft_id.
  """
  try:
    draft = service.users().drafts().get(user_id=user_id, id=draft_id).execute()

    print 'Draft id: %s\nDraft message: %s' % (draft['id'], draft['message'])

    return draft
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

JavaScript

Does not use a client library.

/**
 * Get Draft with given ID.
 *
 * @param  {String} userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  {String} draftId ID of Draft to get.
 * @param  {Function} callback Function to call when the request is complete.
 */
function getDraft(userId, draftId, callback) {
  var request = gapi.client.gmail.users.drafts.get({
    'userId': userId,
    'id': draftId
  });
  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.