【问题标题】:How to upload file to Google drive with Postman using multipart?如何使用多部分使用 Postman 将文件上传到 Google 驱动器?
【发布时间】:2023-03-19 01:13:01
【问题描述】:

如何通过 Postman 使用 Google Drive API 将文件上传到 Google Drive?

我还想设置文件名父目录

我做了什么:

我尝试上传具有以下属性的文件:

但我收到 403 错误:

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "parseError",
                "message": "Parse Error"
            }
        ],
        "code": 400,
        "message": "Parse Error"
    }
}

【问题讨论】:

  • 关于似乎 Postman 无法将 JSON 正文与多部分附件一起发送的最后一条评论表明它无法完成

标签: rest file-upload google-drive-api postman multipart


【解决方案1】:

我已经解决了我的问题,使用这个问题的答案:How to send application/json data along with file in postman multipart/form-data post request?

解决方案

1) 创建上传到 Google Drive 的文件:test.txt

2) 使用 json 格式的文件 metadata properties 创建文件 upload-options.json

{
  "name": "my-uploaded-file.txt",
  "parents": ["<parent-directory-id>"]
}

3) 添加两个form-data 字段:

  • 第一 - upload-options.json
  • - test.txt

【讨论】:

  • 我已经尝试过您在上面发布的相同方式,但出现以下错误,我还在 console.developers.google.com 中添加了我的应用程序所需的范围。 { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission: 请求的身份验证范围不足。" } ], "code": 403, "message": "Insufficient Permission: Request has enough authentication scopes." } }
  • @Krrish 这可能是因为通过 OAuth 获取访问令牌时,您没有提供/请求足够大的范围。每个/大多数驱动器请求都需要不同的范围,它们始终显示在参考资料中。对于 file.create 这将如下所示。 developers.google.com/drive/api/v3/reference/files/create#auth
  • 这是一个不错的方法,但不幸的是,它对我不起作用。即使在以其他方式定义的upload-options.json 中,仍将文件保存为“无标题”。有什么建议吗?
【解决方案2】:

您应该考虑应用的权限范围。 在下面添加行代码帮助您访问对谷歌驱动程序 api 的完全控制

const SCOPES = ['https://www.googleapis.com/auth/drive'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多