如何在没有身份验证的情况下使用 python 上传到谷歌驱动器?
问题描述
我想使用 Google Script 和 Python 将文件上传到 Google Drive.
I want to upload files to Google Drive with Google Script and Python.
我不想使用 API 执行此操作,因为它使用 JSON 文件并请求 Google 帐户的密码.
I don't want to do this with API because it's with JSON file and requesting a password to google account.
我想从没有 JSON 文件且不请求谷歌帐户的 Python 文件发送.
I want to send from the Python file without a JSON file and without requesting a google account.
我想在谷歌脚本中创建一个脚本,将文件上传到网站.
I want to create a script in google script that will upload the file to the site.
我找到了如何使用 html 来做到这一点:
I found how to do it with html:
function saveFile(data,name,folderName) {
var contentType = data.substring(5,data.indexOf(';'));
var file = Utilities.newBlob(
Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)),
contentType,
name
); //does the uploading of the files
DriveApp.getFolderById(childFolderIdA).createFile(file);
}
但是我没有找到如何用 python 来做.
But I did not find how to do it with python.
如何将带有文件的文件发送到此代码?
How do I send file with file to this code?
解决方案
您可以通过发布一个网络应用程序以我"(您)的身份运行并访问:任何人,甚至匿名".
You can do this by publishing a web app to run as "Me"(you) with access: "Anyone, even anonymous".
function doPost(e){
const FOLDER_ID = '###FOLDER_ID###';
var name = e.parameter.name;
saveFile(e.postData.contents,name,FOLDER_ID);
return 'Success';
}
function saveFile(data,name,id) {
data = Utilities.newBlob(Utilities.base64DecodeWebSafe(data));
DriveApp.getFolderById(id)
.createFile(data.setName(name))
}
客户端:
import requests, base64
url = '###WEB_APP_PUBLISHED_URL###'
name = '###FILENAME###'
requests.post(url+'?name='+name,data=base64.urlsafe_b64encode(open('##UPLOAD FILE PATH##','rb').read()))
注意:
- 知道网络应用网址的任何人都可以上传到您的驱动器
- 网络应用指南
- 请求库
相关文章