Skip to content
This repository was archived by the owner on Jul 22, 2021. It is now read-only.
This repository was archived by the owner on Jul 22, 2021. It is now read-only.

Can we use proxy to access google drive ? #109

Open

Description

@karamjaber

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()

Activity

  1. RNabel commented on Aug 18, 2017

    @RNabel
    Contributor

    @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.

  2. karamjaber commented on Aug 23, 2017

    @karamjaber
    Author

    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....

  3. RNabel commented on Aug 23, 2017

    @RNabel
    Contributor

    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 :)

  4. karamjaber commented on Aug 24, 2017

    @karamjaber
    Author

    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
    
    
  5. RNabel commented on Aug 30, 2017

    @RNabel
    Contributor

    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)
  6. karamjaber commented on Aug 31, 2017

    @karamjaber
    Author

    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
    
  7. RNabel commented on Sep 1, 2017

    @RNabel
    Contributor

    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.

  8. zjcnew commented on Mar 21, 2018

    @zjcnew

    How to Login Auth Through SOCK proxy?

  9. NullYing commented on May 28, 2020

    @NullYing

    Read this PR: #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)
  10. fuaoki commented on Aug 13, 2020

    @fuaoki

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions