简体   繁体   English

pydrive:尝试将文件从远程服务器上传到 Google Drive

[英]pydrive: trying to upload files to Google Drive from a remote server

I am trying to upload files to google drive automatically with a python script working remotely from a (Ubuntu) server.我正在尝试使用从(Ubuntu)服务器远程工作的 python 脚本自动将文件上传到谷歌驱动器。

In my code I have the following simple lines from Pydrive:在我的代码中,我有以下来自 Pydrive 的简单行:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file = drive.CreateFile({"parents": [{"kind": "<directory_name>","id": "<directory_id>"}]})
file.SetContentFile('<file_name>')
file.Upload()

I have a setting.yaml file in my python directory so that the script can authenticate itself automatically after the first manual authentication, and a client_secrets.json.我的 python 目录中有一个 setting.yaml 文件,以便脚本可以在第一次手动身份验证后自动进行身份验证,还有一个 client_secrets.json。 I basically followed all the steps in https://pythonhosted.org/PyDrive/oauth.html .我基本上遵循了https://pythonhosted.org/PyDrive/oauth.html中的所有步骤。

However, when I activate my script on the remote Ubuntu server, I am connected to a google page on the terminal environment asking me for my email address and password.但是,当我在远程 Ubuntu 服务器上激活我的脚本时,我连接到终端环境中的谷歌页面,询问我的 email 地址和密码。 When I enter these, Google asks for a second authentication: either look at an image with a code (and nothing shows up on terminal), or "listen and type the number that you hear".当我输入这些时,谷歌会要求进行第二次身份验证:要么查看带有代码的图像(终端上什么都没有显示),要么“听并输入你听到的数字”。 When I type on that command, the terminal replies "is not found" and I cannot hear anything (see picture below).当我输入该命令时,终端回复“未找到”,我听不到任何声音(见下图)。

在此处输入图像描述

I am stuck and I do not know how to circumnavigate this problem.我被卡住了,我不知道如何解决这个问题。 I unfortunately need to authenticate at least once, then the script will authenticate itself automatically.不幸的是,我至少需要进行一次身份验证,然后脚本将自动进行身份验证。 I really do not see how to skip that step.我真的不知道如何跳过这一步。

Any idea / comment / insight would be greatly appreciated任何想法/评论/见解将不胜感激

Thank you so much Berti非常感谢伯蒂

As mentioned in the docs:如文档中所述:

You can also use CommandLineAuth() which manually takes code from user at command line.您还可以使用CommandLineAuth()在命令行中手动从用户那里获取代码。

The LocalWebserverAuth() will spawn a local webserver listening (by default) on localhost:8080, and attempt to open an authentication URL on the user's web browser. LocalWebserverAuth()将在 localhost:8080 上生成本地网络服务器侦听(默认情况下),并尝试在用户的 web 浏览器上打开身份验证 URL。 The opened page will, after authentication, communicate the authentication codes to the local server running on localhost.打开的页面将在身份验证后将身份验证代码传送到在 localhost 上运行的本地服务器。

The problem here is that running this code on your remote server cannot open the link in the browser on your local machine, and thus, you cannot login.这里的问题是在远程服务器上运行此代码无法在本地计算机的浏览器中打开链接,因此您无法登录。

Using CommandLineAuth() will instead print out a URL on the remote server's terminal, which you can open in your local browser.使用CommandLineAuth()将在远程服务器的终端上打印出 URL,您可以在本地浏览器中打开它。 You then authenticate in your browser, which gives you the authentication codes, which you need to copy from your local browser and paste into the prompt in the terminal.然后,您在浏览器中进行身份验证,它会为您提供身份验证代码,您需要从本地浏览器复制并粘贴到终端的提示中。

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.CommandLineAuth()  # <--
drive = GoogleDrive(gauth)

file = drive.CreateFile({"parents": [{"kind": "<directory_name>","id": "<directory_id>"}]})
file.SetContentFile('<file_name>')
file.Upload()

If you want to automatically distinguish between local and remote machines to choose the authentication method, I'd recommend to check out these questions:如果您想自动区分本地和远程机器以选择身份验证方法,我建议您查看以下问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM