Search code examples
asp.net.netgoogle-drive-apigoogle-docsgoogle-docs-api

Getting revision history of a document in .NET using Google Drive API


I am trying to find a way to get revision history for a google doc.

I have the following code which returns the Revisions:

var _driveService = GetDriveServiceInstance();
RevisionList revisions = _driveService.Revisions.List(fileId).Execute();

But, I cannot get the changes made on the document. For example, words added or removed.

I found this resource where they achieved the same task in R:

url <- modify_url(
  url = "https://docs.google.com/feeds/download/documents/export/Export",
  query = list(
    id = fileId,
    revision = revisionId,
    exportFormat = "txt"
  )
)

In this code, they run a query where revisionId and the fileId can be provided. But, I could not find a way to incorporate these parameters into Revisions.List(fileId) in my own code in ASP.NET.

I wonder how can I do this. Is there a way? I could not find any resources on the Internet.


Solution

  • You are using the list method [1] which will retrieve you the list of all revisions with their attributes for each revision, these are specified here [2]. You can use the get method if you want to get a specific revision related to a revision ID [3] .

    As you can see, there is no attribute to know the check if the words were added or removed. But the exportLinks attribute is a JSON with various links to download the file (how it was after the changes on that revision) in different file types (html, pdf, etc) [4].

    In the resource you posted, they use a workaround to obtain the links, because the URL is always in the same format and you only need to change the parameters on the URL (File ID, Revision ID and file type):

     https://docs.google.com/feeds/download/documents/export/Export?id=FileID&revision=RevisionID&exportFormat=FileType
    

    You would have to fetch those URLs to obtain the file in your code, read it and see what changes have been made. Also, keep in mind that if the file is not public for everyone, you’ll need credentials with the right permissions to download the file (via browser or code).

    [1] https://developers.google.com/resources/api-libraries/documentation/drive/v3/csharp/latest/classGoogle_1_1Apis_1_1Drive_1_1v3_1_1RevisionsResource_1_1ListRequest.html

    [2] https://developers.google.com/drive/api/v3/reference/revisions

    [3] https://developers.google.com/resources/api-libraries/documentation/drive/v3/csharp/latest/classGoogle_1_1Apis_1_1Drive_1_1v3_1_1RevisionsResource_1_1GetRequest.html

    [4] https://developers.google.com/resources/api-libraries/documentation/drive/v3/csharp/latest/classGoogle_1_1Apis_1_1Drive_1_1v3_1_1Data_1_1Revision.html