Skip to content

Instantly share code, notes, and snippets.

@ConnorGriffin
Created September 4, 2018 05:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConnorGriffin/43597a3f07b990a4a48a8ef852fb5d8c to your computer and use it in GitHub Desktop.
Save ConnorGriffin/43597a3f07b990a4a48a8ef852fb5d8c to your computer and use it in GitHub Desktop.
Download files from Google Drive, converting to specific MIME types if necessary
# Set the Google Auth parameters. Fill in your RefreshToken, ClientID, and ClientSecret
$params = @{
Uri = 'https://accounts.google.com/o/oauth2/token'
Body = @(
"refresh_token=$RefreshToken", # Replace $RefreshToken with your refresh token
"client_id=$ClientID", # Replace $ClientID with your client ID
"client_secret=$ClientSecret", # Replace $ClientSecret with your client secret
"grant_type=refresh_token"
) -join '&'
Method = 'Post'
ContentType = 'application/x-www-form-urlencoded'
}
$accessToken = (Invoke-RestMethod @params).access_token
# Set the authentication headers
$headers = @{
'Authorization' = "Bearer $accessToken"
'Content-type' = 'application/json'
}
# Change this to the id of the file you want to download. Right click a file and click 'Get shareable link' to find the ID
$fileId = 'fileId'
# Change this to where you want the resulting file to be placed. Just the path; do not include the file name
$destinationPath = 'Path\To\Store\File'
# Get the file metadata
$fileMetadata = Invoke-RestMethod -Uri "https://www.googleapis.com/drive/v3/files/$fileId" -Headers $headers
# If this is a Google Apps filetype, export it
if ($fileMetadata.mimetype -like 'application/vnd.google-apps.*') {
# Determine which mimeType to use when exporting the files
switch ($fileMetadata.mimetype) {
'application/vnd.google-apps.document' {
$exportMime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
$exportExt = '.docx'
}
'application/vnd.google-apps.presentation' {
$exportMime = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
$exportExt = '.pptx'
}
'application/vnd.google-apps.spreadsheet' {
$exportMime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
$exportExt = '.xlsx'
}
'application/vnd.google-apps.drawings' {
$exportMime = 'image/png'
$exportExt = '.png'
}
'application/vnd.google-apps.script' {
$exportMime = 'application/vnd.google-apps.script+json'
$exportExt = '.json'
}
}
$exportFileName = "$destinationPath\$($fileMetadata.name)$exportExt"
Invoke-RestMethod -Uri "https://www.googleapis.com/drive/v3/files/$fileId/export?mimeType=$exportMime" -Method Get -OutFile $exportFileName -Headers $headers
} else {
# If this is a binary file, just download it as-is.
$exportFileName = "$destinationPath\$($fileMetadata.name)"
Invoke-RestMethod -Uri "https://www.googleapis.com/drive/v3/files/${fileId}?alt=media" -Method Get -OutFile $exportFileName -Headers $headers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment