Comments: insert

Stay organized with collections Save and categorize content based on your preferences.

Creates a new comment on the given file. Try it now or see an example.

Request

HTTP request

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

Parameters

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

Authorization

This request allows authorization with at least one of the following scopes:

Scope
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file

Some scopes are restricted and require a security assessment for your app to use them. For more information, see the authentication and authorization page.

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
Optional Properties
context.type string The MIME type of the context snippet. writable
context.value string Data representation of the segment of the file being commented on. In the case of a text file for example, this would be the actual text that the comment is about. 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 {

  // ...

  /**
   * Insert a new document-level comment.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to insert comment for.
   * @param content Text content of the comment.
   * @return The inserted comment if successful, {@code null} otherwise.
   */
  private static Comment insertComment(Drive service, String fileId,
      String content) {
    Comment newComment = new Comment();
    newComment.setContent(content);
    try {
      return service.comments().insert(fileId, newComment).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>
  /// Insert a new document-level comment.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="fileId">ID of the file to insert comment for.</param>
  /// <param name="content">Text content of the comment.</param>
  /// <returns>The inserted comment, null is returned if an API error occurred</returns>
  public static Comment InsertComment(DriveService service, String fileId, String content) {
    Comment newComment = new Comment();
    newComment.Content = content;
    try {
      return service.Comments.Insert(newComment, fileId).Execute();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
    return null;
  }

  // ...

}

PHP

Uses the PHP client library.

/**
 * Insert a new comment.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param String $fileId ID of the file to insert comment for.
 * @param String $content Text content of the comment.
 * @return Google_Service_Drive_Comment The inserted comment. NULL is returned
 *     if an API error occurred.
 */
function insertComment($service, $fileId, $content) {
  $newComment = new Google_Service_Drive_Comment();
  $newComment->setContent($content);
  try {
    return $service->comments->insert($fileId, $newComment);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

Python

Uses the Python client library.

from apiclient import errors
# ...

def insert_comment(service, file_id, content):
  """Insert a new document-level comment.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to insert comment for.
    content: Text content of the comment.
  Returns:
    The inserted comment if successful, None otherwise.
  """
  new_comment = {
      'content': content
  }
  try:
    return service.comments().insert(
        fileId=file_id, body=new_comment).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

JavaScript

Uses the JavaScript client library.

/**
 * Insert a new document-level comment.
 *
 * @param {String} fileId ID of the file to insert comment for.
 * @param {String} content Text content of the comment.
 */
function insertComment(fileId, content) {
  var body = {'content': content};
  var request = gapi.client.drive.comments.insert({
    'fileId': fileId,
    'resource': body
  });
  request.execute(function(resp) { });
}

Try it!

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