Google Drive Picker使用drive.file作用域时会返回401错误,而使用drive.readonly时则能正常工作

前端开发 2026-07-09

我正在用 @googleworkspace/drive-picker-element 在一个Electron应用中集成Google Drive Picker。

我的OAuth流程工作正常:

  1. 打开OAuth URL:

https://accounts.google.com/o/oauth2/v2/auth

  • response_type=code
  • access_type=offline
  • prompt=consent
  • 重定向到本地地址并接收授权码
  • 通过: https://oauth2.googleapis.com/token 兑换代码以获取令牌

我可以成功获取:

  • access_token
  • refresh_token
  • id_token

并且该access token可用于Drive API:

GET https://www.googleapis.com/drive/v3/files

返回数据成功。

问题

当我将该令牌与Drive Picker一起使用时:

<drive-picker
  client-id="..."
  app-id="..."
  oauth-token="ACCESS_TOKEN"
  developer-key="..."
>
  <drive-picker-docs-view
    mode="LIST"
    include-folders
  />
</drive-picker>

Picker会在内部打开:

https://docs.google.com/picker?...

并且此请求返回 401

重要观察

如果我使用以下参数请求OAuth:

https://www.googleapis.com/auth/drive.readonly

Picker可以正常工作。

但如果我使用以下参数请求OAuth:

https://www.googleapis.com/auth/drive.file

Picker始终返回401。

这有些令人困惑,因为Google文档表明Picker支持 drive.file,而像Notion这样的产品也似乎使用 drive.file

已经检查

  • access token有效
  • Drive API可与同一令牌一起工作
  • OAuth代码流程可用
  • client_id, app_id,以及API Key来自同一个Google Cloud项目
  • Google Drive API已启用
  • Google Picker API已启用
  • 授权的JavaScript来源配置正确
  • developer-key 已提供
  • login-hint 已测试(邮箱和子)
  • 在已登录与已登出浏览器状态下均存在相同问题

额外测试的作用域:

  • openid
  • userinfo.email
  • userinfo.profile

没有变化。

问题

Google Drive Picker目前是否支持 drive.filedrive-picker-docs-view / "all" 视图?

使用 drive.file 与Picker一起是否有额外的要求或限制?

在Picker浏览文件时,drive.readonly 是否实质上是必需的?

如能提供澄清,将不胜感激。

谢谢。

解决方案

https://developers.google.com/workspace/drive/picker/guides/overview-desktop 现已成为在桌面应用中使用drive.file作用域的Picker的推荐方式。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章