在Google Drive/PyDrive上传功能中使用代理时是否有人遇到此问题?



我有代码,我将在远程服务器上运行,将文件上传到Google Drive并返回一个可共享的链接。

我遇到的问题是当我尝试使用代理时。GoogleAuth似乎与我使用的代理工作得很好,但GoogleDrive file.Upload()…其实不然。我尝试了所有可用的解决方案,并注意到没有人在任何地方真正正确地回答这个问题。我尝试将代理作为httplib2对象传入,如下所示:

file.Upload(params={"http": proxy})

然而,它不起作用。在远程机器上,它基本上永远运行,不显示任何输出/错误……所以调试是很烦人的。

下面是我使用的代码:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import httplib2
from httplib2 import socks
from datetime import date
import requests
import json
today = date.today()
formatDate = today.strftime("%m%d%y")
file = './index.html'
folderId = "1cZtQ33rest_of_the_folderid"
sharedDriveId = '0AP3cwhrest_of_the_sharedid'
filename = "{0}_Index1.html".format(formatDate)
host = "myproxygoeshere"
port = 8080
gauth = GoogleAuth()
# Setting up proxy httplib2 object
http = httplib2.ProxyInfo(proxy_type=httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL,
proxy_host=host,
proxy_port=port)
gauth.LoadCredentialsFile("credentials.json")
gauth.http = httplib2.Http(proxy_info=http)
if gauth.credentials is None:
gauth.GetFlow()
gauth.flow.params.update({'access_type': 'offline'})
gauth.flow.params.update({'approval_prompt': 'force'})
gauth.CommandLineAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("credentials.json")
drive = GoogleDrive(gauth)

#print(drive.GetAbout()) - note, if i uncomment this line on the remote machine, this is where the program will go on a never-ending loop with no output.. just running. If it's commenting, the next point it will do that is the file.Upload() line further down.
# Setting the file metadata, file content, folder/shared drive and uploading.
file1 = drive.CreateFile(metadata={"title": "{0}_IndexTEST2.txt".format(formatDate), 'mimeType': 'text/html', "parents":
[{"kind": 'drive#fileLink', 'myteamDriveId': sharedDriveId, "id": folderId}]})
print("File created")
#file1.SetContentFile("index.html")
file1.SetContentString("Testing")# Set content of the file from given string.
print("Content set")
try:
file1.Upload(param={'supportsAllDrives': True}) # Right here is where it freezes (or is running, but doesn't do anything.. just a blank line in terminal)/runs forever and shows zero output.
except Exception as e:
print("ERROR: ", e)
#file1.Upload(param={'supportsAllDrives': True, "http": proxy_info})
print("File uploaded.")

# Allowing access to get a shareable link, using the Drive API directly
# (not PyDrive) (ONLY MEMBERS OF THE SHARED DRIVE CAN ACCESS):
access_token = gauth.credentials.access_token
file_id = file1['id']
url = 'https://www.googleapis.com/drive/v3/files/' + file_id + '/permissions?supportsAllDrives=true'
headers = {'Authorization': 'Bearer ' + access_token, 'Content-Type': 'application/json'}
payload = {'type': 'anyone', 'value': 'anyone', 'role': 'reader'}
res = requests.post(url, data=json.dumps(payload), headers=headers)

# SHARABLE LINK
link = file1['alternateLink']
print(link)

我在代码中设置代理的方式似乎存在某种错误。导出变量http_proxy和https_proxy,并将值作为代理主机&端口,然后删除我的代码中所有代理相关的代码,并从那里运行程序在代理上完美地工作,如预期的。

因此,为了将来对其他人的参考-如果它不适合您,并且您试图使用PyDrive的代理,您应该尝试删除所有与您编写的代理相关的代码,并在命令行中尝试:

export http_proxy={proxyhost:proxyport到这里}导出https_proxy={如上所述}

然后尝试运行您的代码。这对我很有效!

最新更新