United Kingdom: +44 (0)208 088 8978

Google Apis for .NET in Fsharp

Interact with Google API's easily using .NET and @fsharp. In this week's blog post we run through an example of how to get started with this.

We're hiring Software Developers

Click here to find out more

Recently I was asked to investigate how we can create custom slideshows from template slideshows. There are various libraries out there in the .NET space that can do this for us. One of the options that was probably not obvious is Google Slides and the related Google APIs.

Google have created a set of very handy packages that allow us to perform all sorts of creation/editing using a combination of Drive, Sheets and Slides APIs that interact with Google via HTTP.

So in this article we are going to start with something very simple and copy an existing Google Drive file and give it a new name.


I will be using a new console application using .NET 6 (the project that this investigation is for uses this) and will install the following packages

dotnet new console -f net6.0 -lang F# -n GoogleApisExample

dotnet add package Google.Apis
dotnet add package Google.Apis.Auth
dotnet add package Google.Apis.Drive.v3

Set up Google Cloud

Next we will need to set up an application in the Google Cloud Platform, set up Oauth2 authentication for the application and download a secrets.json file that our application will use to authenticate when running.

I will not cover this here but there will be links below that cover these topics

A note on scopes. For this example we will need access to Google Drives scope whilst creating the OAuth2 authentication. In this case https://www.googleapis.com/auth/drive. Each individual Google API has a range of access scopes that are required to be configured but again see the Google docs for more information.

When all of this is setup the main item you need from the Cloud Platform is the the OAuth client which is in the form of a json file.

For this example I have renamed this file client-secrets.json and have place this file in /bin/Debug/net6.0 folder of our application.


With that in place our application can get the necessary permissions to access our Google Drive.

let secret = GoogleClientSecrets.FromFile("client-secret.json").Secrets

let path = Path.Combine("credentials")

let scopes = [| "https://www.googleapis.com/auth/drive" |]

let credentials =
    task {
        return! GoogleWebAuthorizationBroker.AuthorizeAsync(
            <email of your Google account here>,
            FileDataStore(path, true))
    |> Async.AwaitTask
    |> Async.RunSynchronously

Upon running this a page should open in our browser asking us to login to our Google account and then to grant access to the application. Note that this only happens the first time we run this code and if the access credentials has expired/been removed or revoked.

Upon continuing you can close the browser and the appropriate credentials will be created in the folder /bin/Debug/net6.0/credentials

Client Service

Next we create the ClientService using the returned credentials.

let clientService = BaseClientService.Initializer(
    HttpClientInitializer = credentials,
    ApplicationName = "test-slides-api")

This ClientService is then used to create any of the API-specific services that we need to interact with. In the following example this will be the DriveService for Google Drive.

Interacting with Google Drive

let driveService = new DriveService(clientService)

Next we need to get the id of an existing file that we have in our Google Drive. We can find this by opening the file and looking at the url in the browser.

Then we can use this to tell Google Drive to copy this file and we can give the new file a name. Finally we print the new file id to the console

let existingFileId = <id from the url>

let newFile = File(Name = "copy-file")

let file =
    task {
        return! driveService.Files.Copy(newFile, existingFileId).ExecuteAsync()
    |> Async.AwaitTask
    |> Async.RunSynchronously

printf $"Created new file with Id {file.Id}"

You should now see the new file has been created on Google Drive in the same folder as the existing file and if you open it the id should be the same as is in the output of the console. 🙂


So using the provided Google libraries we can quickly and easily start interacting with Google APIs. Examples of this could be

  • Replacing text and images in existing slideshows using Google Slides
  • Creating data in spreadsheets using Google Sheets
  • Creating charts and then using them on an existing slideshow using Google Slides and Sheets together

A copy of this code is available at https://github.com/martinbryant/google-apis-example