Users.threads: modify

Requires authorization

Modifies the labels applied to the thread. This applies to all messages in the thread. Try it now or see an example.

Request

HTTP request

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

Parameters

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

Request body

In the request body, supply data with the following structure:

{
  "addLabelIds": [
    string
  ],
  "removeLabelIds": [
    string
  ]
}
Property name Value Description Notes
addLabelIds[] list A list of IDs of labels to add to this thread.
removeLabelIds[] list A list of IDs of labels to remove from this thread.

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.ModifyThreadRequest;
import com.google.api.services.gmail.model.Thread;

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

// ...

public class MyClass {

  // ...

  /**
   * Modify the Labels applied to a Thread..
   *
   * @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 the thread within the user's account.
   * @param labelsToAdd List of label ids to add.
   * @param labelsToRemove List of label ids to remove.
   * @throws IOException
   */
  public static void modifyThread(Gmail service, String userId, String threadId,
      List<String> labelsToAdd, List<String> labelsToRemove) throws IOException {
    ModifyThreadRequest mods = new ModifyThreadRequest().setAddLabelIds(labelsToAdd)
        .setRemoveLabelIds(labelsToRemove);
    Thread thread = service.users().threads().modify(userId, threadId, mods).execute();

    System.out.println("Thread id: " + thread.getId());
    System.out.println(thread.toPrettyString());
  }

  // ...

}

.NET

Uses the .NET client library.

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

using System.Collections.Generic;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// Modify the Labels applied to a Thread.
  /// </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 the Thread to modify.</param>
  /// <param name="labelsToAdd">List of label ids to add.</param>
  /// <param name="labelsToRemove">List of label ids to remove.</param>
  public static Thread ModifyThread(GmailService service, String userId,
      String threadId, List<String> labelsToAdd, List<String> labelsToRemove)
  {
      ModifyThreadRequest mods = new ModifyThreadRequest();
      mods.AddLabelIds = labelsToAdd;
      mods.RemoveLabelIds = labelsToRemove;

      try
      {
          return service.Users.Threads.Modify(mods, userId, threadId).Execute();
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }

      return null;
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Modify the Labels applied to a Thread.
 *
 * @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 modify.
 * @param  array $labelsToAdd Array of Labels to add.
 * @param  array $labelsToRemove Array of Labels to remove.
 * @return Google_Service_Gmail_Thread Modified Thread.
 */
function modifyThread($service, $userId, $threadId, $labelsToAdd, $labelsToRemove) {
  $mods = new Google_Service_Gmail_ModifyThreadRequest();
  $mods->setAddLabelIds($labelsToAdd);
  $mods->setRemoveLabelIds($labelsToRemove);
  try {
    $thread = $service->users_threads->modify($userId, $threadId, $mods);
    print 'Thread with ID: ' . $threadId . ' successfully modified.';
    return $thread;
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }
}

Python

Uses the Python client library.

"""Modify a Thread.
"""

from apiclient import errors


def ModifyThread(service, user_id, thread_id, msg_labels):
  """Add labels to 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 to be modified.
    msg_labels: The change in labels.

  Returns:
    Thread with modified Labels.
  """
  try:
    thread = service.users().threads().modify(userId=user_id, id=thread_id,
                                              body=msg_labels).execute()

    thread_id = thread['id']
    label_ids = thread['messages'][0]['labelIds']

    print 'Thread ID: %s - With Label IDs %s' % (thread_id, label_ids)
    return thread
  except errors.HttpError, error:
    print 'An error occurred: %s' % error


def CreateMsgLabels():
  """Create object to update labels.

  Returns:
    A label update object.
  """
  return {'removeLabelIds': [], 'addLabelIds': ['UNREAD', 'Label_2']}

JavaScript

Does not use a client library.

/**
 * Modify the Labels a Thread is associated with.
 *
 * @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 modify.
 * @param  {Array} labelsToAdd Array of Labels to add.
 * @param  {Array} labelsToRemove Array of Labels to remove.
 * @param  {Function} callback Function to call when the request is complete.
 */
function modifyThread(userId, threadId, labelsToAdd, labelsToRemove, callback) {
  var request = gapi.client.gmail.users.threads.modify({
    'userId': userId,
    'id': threadId,
    'addLabelIds': labelsToAdd,
    'removeLabelIds': labelsToRemove
  });
  request.execute(callback);
}

Try it!

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

フィードバックを送信...

ご不明な点がありましたら、Google のサポートページをご覧ください。