PyDrive icon indicating copy to clipboard operation
PyDrive copied to clipboard

Can we use proxy to access google drive ?

Open karamjaber opened this issue 8 years ago • 10 comments

I want to use the pydrive with proxy. Is it supported by pyDrive ? The below example doesn't work because i need a proxy connection with googleAuth:

from pydrive.drive import GoogleDrive

drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given string.
file1.Upload()

karamjaber avatar Aug 17 '17 10:08 karamjaber

@karamjaber this is indeed possible.

Each connection is made with an httplib2 http object (see this line for where it is initialised). You will need to initialise a custom http object (you can use this StackOverflow answer as a starting point) and pass the resulting object to the Upload function.

Your code will look something like the following:


from pydrive.drive import GoogleDrive
import httplib2
from httplib2 import socks

# Create proxied http object (taken from StackOverflow answer).
http = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 
  <proxy host address>, 8080, 
  proxy_user = <proxy user id>, 
  proxy_pass = <proxy password>))

# Setup drive.
drive = GoogleDrive(gauth)

# Setup file object.
file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given string.

# Upload file using the proxied http connection.
file1.Upload(param={"http": http})

Note: The above code is not tested. If the above doesn't work as expected 1) make sure the proxy connection works using something like resp, content = http.request("http://google.com", "GET"). If the proxy works but PyDrive breaks make sure to post the error message.

RNabel avatar Aug 18 '17 13:08 RNabel

Is there any other way that doesn't use httplib2 ? because socks is alwayes None... and when i change it to the number 3 it doesn't work....

karamjaber avatar Aug 23 '17 11:08 karamjaber

No, it is not possible to use another library, unfortunately. PyDrive would need to be rewritten to replace the httplib2 dependency.

It sounds like you have some problems with your imported packages. Substituting socks.PROXY_TYPE_HTTP with 3 (due to import errors) should do the trick. If you would like further help with this, feel free to post the exact error you are getting.

For future reference, you usually include 4 things in a bug or issue report: 1) Summary, 2) steps to reproduce, 3) expected result, and 4) actual results. That way it's easier to find out what you are talking about and be able to help you better :)

RNabel avatar Aug 23 '17 18:08 RNabel

It seems like that defining a proxy with httplib2 is not working, the code that i run is :

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

import httplib2
# from httplib2 import socks

# Create proxied http object (taken from StackOverflow answer).
http = httplib2.Http(proxy_info = httplib2.ProxyInfo(3,myProxy, myPort))
gauth = GoogleAuth()
gauth.http = httplib2.Http(proxy_info = httplib2.ProxyInfo(3,myProxy, myPort))
print(9)
gauth.LocalWebserverAuth()
# Setup drive.
print(10)
drive = GoogleDrive(gauth)

# Setup file object.
print(1)
file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given string.
print(2)
# Upload file using the proxied http connection.
file1.Upload(param={"http": http})

The expected behaviour is that after this code had run , a file with the name Hello.txt and content Hello word will be uploaded to google drive but the above code just prints 9 and then opens the browser for authentication. after manually authenticating an error message appears :

Traceback (most recent call last):
  File "Tiota.py", line 12, in <module>
    gauth.LocalWebserverAuth()
  File "C:\PYTHON35\lib\site-packages\pydrive\auth.py", line 125, in _decorated
    self.Auth(code)
  File "C:\PYTHON35\lib\site-packages\pydrive\auth.py", line 495, in Auth
    self.Authenticate(code)
  File "C:\PYTHON35\lib\site-packages\pydrive\auth.py", line 508, in Authenticat
e
    self.credentials = self.flow.step2_exchange(code)
  File "C:\PYTHON35\lib\site-packages\oauth2client\_helpers.py", line 133, in po
sitional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\PYTHON35\lib\site-packages\oauth2client\client.py", line 2054, in ste
p2_exchange
    http, self.token_uri, method='POST', body=body, headers=headers)
  File "C:\PYTHON35\lib\site-packages\oauth2client\transport.py", line 282, in r
equest
    connection_type=connection_type)
  File "C:\PYTHON35\lib\site-packages\httplib2\__init__.py", line 1322, in reque
st
    (response, content) = self._request(conn, authority, uri, request_uri, metho
d, body, headers, redirections, cachekey)
  File "C:\PYTHON35\lib\site-packages\httplib2\__init__.py", line 1072, in _requ
est
    (response, content) = self._conn_request(conn, request_uri, method, body, he
aders)
  File "C:\PYTHON35\lib\site-packages\httplib2\__init__.py", line 995, in _conn_
request
    conn.connect()
  File "C:\PYTHON35\lib\http\client.py", line 1252, in connect
    super().connect()
  File "C:\PYTHON35\lib\http\client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "C:\PYTHON35\lib\socket.py", line 711, in create_connection
    raise err
  File "C:\PYTHON35\lib\socket.py", line 702, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected
 party did not properly respond after a period of time, or established connectio
n failed because connected host has failed to respond

karamjaber avatar Aug 24 '17 08:08 karamjaber

Thank you for sending all of this info, and I'm sorry to hear that it is still not working.

Could you try whether you can request any website at all (rather than trying to use your http object in PyDrive)?

e.g. if you insert this line after you create the http object, can you tell me what it prints?

resp, content = http.request("http://google.com", "GET")
print(resp)
print(content)

RNabel avatar Aug 30 '17 18:08 RNabel

No. An error message appears(timeour error), it seems like that it tries to send the request without the proxy:

TimeoutError: [WinError 10060] A connection attempt failed because the connected
 party did not properly respond after a period of time, or established connectio
n failed because connected host has failed to respond

karamjaber avatar Aug 31 '17 13:08 karamjaber

Hmm. It looks like there is a problem with your proxy setup (or connection code). Since this is not technically related to this library and since I don't have expertise in setting up python proxies, you may be best off trying to post this question on StackOverflow or the httplib2 github page.

RNabel avatar Sep 01 '17 19:09 RNabel

How to Login Auth Through SOCK proxy?

zjcnew avatar Mar 21 '18 16:03 zjcnew

Read this PR: https://github.com/gsuitedevs/PyDrive/pull/198

and it work now

# -*- coding: utf-8 -*-
import httplib2
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
proxy_info = httplib2.ProxyInfo(proxy_type=httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL,
                                proxy_host='localhost',
                                proxy_port=1080)
gauth.http = httplib2.Http(proxy_info=proxy_info)
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

if __name__ == '__main__':
    print(drive)

NullYing avatar May 28 '20 16:05 NullYing

The authentication is working, but how can I use proxy in drive.ListFile GetList() and drive.CreateFile Upload() functions?

fuaoki avatar Aug 13 '20 13:08 fuaoki