Google Drive SDK

Comments: update

Updates an existing comment. Try it now or see an example.

Request

HTTP request

PUT https://www.googleapis.com/drive/v2/files/fileId/comments/commentId

Parameters

Parameter name Value Description
Path parameters
commentId string The ID of the comment.
fileId string The ID of the file.

Request body

In the request body, supply a Comments resource with the following properties:

Property name Value Description Notes
Required Properties
content string The plain text content used to create this comment. This is not HTML safe and should only be used as a starting point to make edits to a comment's content. writable

Response

If successful, this method returns a Comments 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.drive.Drive;
import com.google.api.services.drive.model.Comment;

import java.io.IOException;
// ...

public class MyClass {

  // ...

  /**
   * Update a comment's content.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to update comment for.
   * @param commentId ID of the comment to update.
   * @param newContent The new text content for the comment.
   * @return The updated comment if successful, {@code null} otherwise.
   */
  private static Comment updateComment(Drive service, String fileId,
      String commentId, String newContent) {
    try {
      // First retrieve the comment from the API.
      Comment comment = service.comments().get(
          fileId, commentId).execute();
      comment.setContent(newContent);
      return service.comments().update(
          fileId, commentId, comment).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
    return null;
  }

  // ...

}

.NET

Uses the .NET client library

using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;

using System.Net;
// ...

public class MyClass {

  // ...

  /// <summary>
  /// Update a comment's content.
  /// </summary>
  /// Drive API service instance.
  /// ID of the file to update comment for.
  /// ID of the comment to update.
  /// The new text content for the comment.
  /// The updated comment, null is returned if an API error occurred
  public static Comment UpdateComment(DriveService service, String fileId,
      String commentId, String newContent) {
    try {
      // First retrieve the comment from the API.
      Comment comment = service.Comments.Get(fileId, commentId).Fetch();
      comment.Content = newContent;
      return service.Comments.Update(comment, fileId, commentId).Fetch();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
    return null;
  }

  // ...

}

PHP

Uses the PHP client library

/**
 * Update a comment's content.
 *
 * @param Google_DriveService $service Drive API service instance.
 * @param String $fileId ID of the file to update comment for.
 * @param String $commentId ID of the comment to update.
 * @param String $newContent The new text content for the comment.
 * @return Google_Comment The updated comment. NULL is returned if an API error occurred.
 */
function updateComment($service, $fileId, $commentId, $newContent) {
  try {
    // First retrieve the comment from the API.
    $comment = $service->comments->get($fileId, $commentId);
    $comment->setContent(newContent);
    return $service->comments->update($fileId, $commentId, $comment);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

Python

Uses the Python client library

from apiclient import errors
# ...

def update_comment(service, file_id, comment_id, new_content):
  """Update a comment's content.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to update comment for.
    comment_id: ID of the comment to update.
    new_content: The new text content for the comment.

  Returns:
    The updated comment if successful, None otherwise.
  """
  try:
    # First retrieve the comment from the API.
    comment = service.comments().get(
        fileId=file_id, commentId=comment_id).execute()
    comment['content'] = new_content
    return service.comments().update(
        fileId=file_id, commentId=comment_id, body=comment).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

Ruby

Uses the Ruby client library

##
# Update a comment's content
#
# @param [Google::APIClient] client
#   Authorized client instance
# @param [String] file_id
#   ID of the file to update comment for
# @param [String] comment_id
#   ID of the comment to update
# @param [String] new_content
#   The new text content for the comment
# @return [Google::APIClient::Schema::Drive::V2::Comment]
#   The updated comment if successful, nil otherwise
def update_comment(client, file_id, comment_id, new_content)
  drive = client.discovered_api('drive', 'v2')
  # First retrieve the comment from the API.
  result = client.execute(
    :api_method => drive.comments.get,
    :parameters => {
      'fileId' => file_id,
      'commentId' => comment_id
    })
  if result.status == 200
    comment = result.data
    comment.content = new_content
    result = client.execute(
      :api_method => drive.comments.update,
      :body_object => comment,
      :parameters => {
        'fileId' => file_id,
        'commentId' => comment_id
      })
    if result.status == 200
      return result.data
    end
  end
  puts "An error occurred: #{result.data['error']['message']}"
end

JavaScript

Uses the JavaScript client library

/**
 * Update a comment's content.
 *
 * @param {String} fileId ID of the file to update comment for.
 * @param {String} commentId ID of the comment to update.
 * @param {String} newContent The new text content for the comment.
 */
function updateComment(fileId, commentId, newContent) {
  // First retrieve the comment from the API.
  var request = gapi.client.drive.comments.get({
    'fileId': fileId,
    'commentId': commentId
  });
  request.execute(function(resp) {
    resp.content = newContent;
    var updateRequest = gapi.client.drive.comments.update({
      'fileId': fileId,
      'commentId': commentId,
      'resource': resp
    });
    updateRequest.execute(function(resp) { });
  });
}

Try it!

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

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.