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 in the folder, but converting those .gdoc files to OneDrive Word files (.docx) using Azure Logic App isn’t straightforward because we don’t have a suitable connector to do so.
For this reason, we decided to create an Azure Function to serve as our connector for this transformation.
📝 One-Minute Brief
Converting Google Docs files inside integration workflows is not straightforward with Logic Apps alone. This article shows how to use an Azure Function to convert Google Documents (.gdoc) into Word documents (.docx), enabling seamless file transformations between Google Drive and Microsoft ecosystems.
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:
- The Azure Function is triggered from the Logic App (can be another method) by an HTTP POST request ([HttpTrigger(AuthorizationLevel.Anonymous, “post”, Route = null)]).
- 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.
- If either the fileId or X-Secret-Google-Credentials is missing or empty, the function returns a BadRequest response indicating the missing information.
- If the required parameters are provided, the function loads the service account credentials from the provided JSON file (GoogleCredential.FromJson(googleCredentialsJson)).
- The function creates Drive and Docs services using the service account credentials.
- 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.
- The stream is used to create an HttpResponseMessage with the exported document as the content.
- 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 the DOCX format using the Google Docs API and returns the converted file as 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 to 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.