Azure Function to convert Google Documents (.gdoc) into Word Documents (.docx)

In Google Web UI Perspective, we have the option to download the Google Docs (word) files in terms of the formats. From the Logic App perspective, we can easily list files from the folder, but converting that .gdoc files into Onedrive word files (.docx extension) using Azure Logic App is not a straightforward task since we don’t have a  suitable connector to achieve it.

For this reason, we decide to create the Azure Function that will act as our connector to archive this transformation.

Google Documents (.gdoc) into Word Documents (.docx) Converter Azure Function

This is a simple function that will be able to convert a .gdoc file inside Google Drive into a base64 .docx encoded file.

The Azure Function will include the following NuGet packages:

  • Google.Apis.Auth (Version 1.58.0 or later): This package provides authentication and authorization functionality for accessing Google APIs.
  • Google.Apis.Docs.v1 (Version 1.58.0 or later): This package provides the Google Docs API client library, allowing you to interact with Google Docs.
  • Google.Apis.Drive.v3 (Version 1.58.0 or later): This package provides the Google Drive API client library, enabling you to interact with Google Drive.

And basically, what this function will do is:

  1. The Azure Function is triggered from the Logic App (can be another method) by an HTTP POST request ([HttpTrigger(AuthorizationLevel.Anonymous, “post”, Route = null)]).
  2. The function expects the fileId to be provided as a query parameter in the request URL and the X-Secret-Google-Credentials header to contain the Google service account credentials in JSON format.
  3. If either the fileId or X-Secret-Google-Credentials is missing or empty, the function returns a BadRequest response indicating the missing information.
  4. If the required parameters are provided, the function loads the service account credentials from the provided JSON file (GoogleCredential.FromJson(googleCredentialsJson)).
  5. The function creates Drive and Docs services using the service account credentials.
  6. It then makes a request to export the specified Google Docs file (service.Files.Export(googleDocsFileId, “application/vnd.openxmlformats-officedocument.wordprocessingml.document”)) and retrieves the resulting document as a stream.
  7. The stream is used to create an HttpResponseMessage with the exported document as the content.
  8. The response headers are set to indicate that the response content should be treated as a downloadable attachment with the file name “converted.docx” and the MIME type “application/vnd.openxmlformats-officedocument.wordprocessingml.document”.

Overall, the Azure Function converts a Google Docs file to a DOCX file format using the Google Docs API and returns the converted file in base64 in the HTTP response.

To use this function, you need to:

  • Pass the fileID as a query parameter to the Azure Function.
    • fileId=<Id>
  • And then, set the X-Secret-Google-Credentials header with this JSON format containing the Google credentials -> you can find this info when you create a private key on your Google service account.
{
   "type": "xxxxx",
   "project_id": "xxxxx",
   "private_key_id": "xxxxx",
   "private_key": "xxxxx",
   "client_email": "xxxxx",
   "client_id": "xxxxx",
   "auth_uri": "xxxxx",
   "token_uri": "xxxxx",
   "auth_provider_x509_cert_url": "xxxxx",
   "client_x509_cert_url": "xxxxx",
   "universe_domain": "xxxxx"
}

Where can I download it?

You can download the complete Azure Functions source code here:

Hope you find this helpful! So, if you liked the content or found it helpful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego! 

Big thanks to my team member Luís Rigueira for realizing this idea.

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top