This post was originally published here
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:
- 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 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=
- 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.