【问题标题】:Google Drive Api, Downloading a file using powershellGoogle Drive Api,使用 powershell 下载文件
【发布时间】:2017-01-10 15:26:58
【问题描述】:

我能够从 google drive api 下载文件,但是,它只适用于文本文件。

如何下​​载其他类型的文件?

示例:word 文件已下载,但当我打开它时,我得到了

“很抱歉,我们无法打开文件,因为我们发现文件内容有问题”

这是代码。谢谢。

function getfile($uri){
    $results = Invoke-RestMethod `
    -Method GET `
    -Uri $uri `
    -Headers @{Authorization = Get-Token} `
    -ContentType application/json

    return $results
}

$fileId = "word document id"
$fileproperties = getfile -uri "https://www.googleapis.com/drive/v3/files/$fileId"
$actualfile = getfile -uri "https://www.googleapis.com/drive/v3/files/$($fileId)?alt=media"

$actualfile | out-file c:..\Desktop\$($fileproperties.name)

【问题讨论】:

  • 您的 getfile 函数为所有请求返回 json 内容类型。这是基于文本的。如果您尝试以 json 格式返回 word 文档,则返回的内容可能已损坏。我建议将内容类型设为函数参数,以便在下载媒体时指定正确的 mime 类型

标签: powershell google-drive-api


【解决方案1】:

我发现如果我使用-OutFile,它可以成功下载。
这仅适用于 Word 和 Excel 文档。

Invoke-RestMethod `
  -Method GET `
  -Uri "https://www.googleapis.com/drive/v3/files/$($file.id)?alt=media" `
  -Headers @{Authorization = Get-Token} `
  -OutFile "C:fileLocation\$($file.name)"

【讨论】:

    猜你喜欢
    • 2015-02-06
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多