我希望编写一个服务器应用程序来将文件上传到Google云端硬盘.我以前使用的是Documents List API,但我看到它已被弃用.我想转到Google Drive API,但这似乎非常局限于使用网络/ OAuth流程.
我需要做的就是将Word,Excel文件等上传到Google云端硬盘,但我需要以完全自动化的方式完成此操作,没有任何用户界面.我希望编写一个命令行应用程序,它可以在cron或其他任何东西上运行,而不需要通过Web等人工干预.
我宁愿放弃Documents List API,因为我不想在最终关闭它时被烧毁,我想使用一个支持的API,谷歌不会很快摆脱它.这存在吗?
谢谢
加里
您的应用程序需要用户的许可才能对其文件使用API.该授权需要使用基于Web的Oauth进行.该授权的结果是您的服务器应用程序最终会有一个可以存储的刷新令牌.您的应用可以随时将该刷新令牌转换为访问令牌并访问驱动器文件.
因此,如果您接受需要进行一次性授权,您就可以实现所需的目标.
gdrive解决方案目前不起作用(登录问题)。所以你现在可以使用rclone. 你可以安装它
conda install -c conda-forge rclone
Run Code Online (Sandbox Code Playgroud)
然后按照配置文档https://rclone.org/drive/
配置后,您将能够使用此命令复制到谷歌驱动器(统计标志用于进度条)
rclone copy <filename> remote: --stats-one-line -P --stats 2s
Run Code Online (Sandbox Code Playgroud)
rclone 包含许多后端,因此您不仅可以上传到谷歌驱动器
由于gdrive 工具不再维护。我找到了一个官方的更好的方法。
使用卷曲。
sudo apt install curl
创建凭据 > 配置 OAth 同意屏幕(如果需要) > 应用程序类型 > 电视和有限输入设备 > 保存您的客户端 ID 和客户端密钥。
curl -d "client_id=<client_id>&scope=https://www.googleapis.com/auth/drive.file" https://oauth2.googleapis.com/device/code
预期回应:
{"device_code": "<long string>",
"user_code": "xxx-xxx-xxx",
"expires_in": 1800,
"interval": 5,
"verification_url": "https://www.google.com/device"}
Run Code Online (Sandbox Code Playgroud)
然后进入https://www.google.com/device-->输入"user_code"-->授予相关权限。
"device_code"和"user_code"值。curl -d client_id=<client id> -d client_secret=<client secret> -d device_code=<device code> -d grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code https://accounts.google.com/o/oauth2/token
预期输出:
{
"access_token": ".....",
"expires_in": 3599,
"refresh_token": "....",
"scope": "https://www.googleapis.com/auth/drive.file",
"token_type": "Bearer"
}
Run Code Online (Sandbox Code Playgroud)
保存该"access_token"值。
开始上传
curl -X POST -L -H "Authorization: Bearer <enter access_token here>" -F "metadata={name :'filename.zip'};type=application/json;charset=UTF-8" -F "file=@filename.zip;type=application/zip" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
警告
上传前将文件制作为 zip 存档。上面的代码适用于存档文件。我用上面的方法成功上传了10gb的压缩包。
小智 6
这是使用GitHub的 Windows,Linux和MacOS的说明| gdrive。
$ gdrive list
Go to the url... enter the oauth verification code... OK
$ gdrive upload file
$ gdrive mkdir UploadDir
ID_of_UploadDir
$ gdrive sync upload LocalDir ID_of_UploadDir
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30559 次 |
| 最近记录: |