如何在没有身份验证的情况下使用 python 上传到谷歌驱动器?

How to upload to google drive with python without authentication?(如何在没有身份验证的情况下使用 python 上传到谷歌驱动器?)
本文介绍了如何在没有身份验证的情况下使用 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()))

注意:

  • 知道网络应用网址的任何人都可以上传到您的驱动器
    • 网络应用指南
    • 请求库

    这篇关于如何在没有身份验证的情况下使用 python 上传到谷歌驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)