↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

How to download files from Google Drive using Elixir

The post explains a small workflow for reading a CSV file from Google Drive in Elixir. It starts by setting up a service account, saving the JSON credentials in the project, and sharing the target Drive folder with the service account email.

Reading notes
#

  • create a service account in Google Developer Console and save the JSON credentials file in the Elixir project
  • give the service account access to the domain accounts if needed, though the post says this step may not be necessary
  • share the Drive folder that contains the files with the service account email
  • add google_api_drive and goth to mix.exs
  • configure goth in config.exs to read the JSON credentials file
  • request a token for the Google Drive scope with Goth.Token.for_scope("https://www.googleapis.com/auth/drive")
  • create a Drive connection with the token
  • list files with drive_files_list/2 using includeItemsFromAllDrives, supportsAllDrives, and corpora: "allDrives"
  • filter the returned files for text/csv, take the first one, and keep its id and name
  • fetch the file body with drive_files_get/4 using mimeType: "text/csv", alt: "media", and decode: false
  • the final result is the downloaded body of the CSV file