yos*_*osi 6 python web-applications google-apps-script google-drive-api
我想使用 Google 脚本和 Python 将文件上传到 Google Drive。
我不想使用 API 执行此操作,因为它使用 JSON 文件并请求 google 帐户的密码。
我想从没有 JSON 文件的 Python 文件发送,并且不请求 google 帐户。
我想在谷歌脚本中创建一个脚本,将文件上传到网站。
我发现如何用 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);
}
Run Code Online (Sandbox Code Playgroud)
但我没有找到如何用 python 做到这一点。
如何将文件与文件发送到此代码?
您可以通过发布一个无头 Web 应用程序(没有 UI)来以“我”(您)的身份运行并具有访问权限:“任何人,甚至匿名”来实现此目的。
function doPost(e){
const FOLDER_ID = '###FOLDER_ID###';
const name = e.parameter.name;
saveFile(e.postData.contents,name,FOLDER_ID);
return ContentService.createTextOutput('Success');
}
function saveFile(data,name,id) {
const blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(data));
DriveApp.getFolderById(id)
.createFile(blob.setName(name))
}
Run Code Online (Sandbox Code Playgroud)
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()))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2526 次 |
最近记录: |