6

I'm currently running a script to pull data from Google Analytics with Python package (that is based on client object)

--> My script works perfectly without any proxy.

But I have to put it behind my corporate proxy, so I need to adapt my httplib2.Http() object to embed proxy information.

Following httplib2 doc 1 I tried:

pi = httplib2.proxy_info_from_url('http://user:pwd@someproxy:80')
httplib2.Http(proxy_info=pi).request("http://www.google.com")

But it did not work. I always get a Time out error, with or without the proxy info (so proxy_info in parameter is not taken into account)

I also downloaded socks in PySocks package (v1.5.6) and tried to "wrapmodule" httplib2 as described in here: https://github.com/jcgregorio/httplib2/issues/205

socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, "proxyna", port=80, username='p.tisserand', password='Telematics12')
socks.wrapmodule(httplib2)
h = httplib2.Http()
h.request("http://google.com")

But I get an IndexError: (tuple index out of range)

In the meantime, When I use the package, this simple code works perfectly:

os.environ["HTTP_PROXY"] = "http://user:pwd@someproxy:80"
req = requests.get("http://www.google.com")

The problem is that need to fit with googleapiclient requirements and provide a htpplib2.Http() client object.

U3.1415926
  • 812
  • 12
  • 30
Phil27
  • 233
  • 1
  • 3
  • 12

2 Answers2

4

rather than using Python2, I think you'd better try using httplib2shim

You can have a look at this tutorial on my blog : https://dinatam.com/fr/python-3-google-api-proxy/

In simple words, just replace this kind of code :

from httplib2 import Http
http_auth = credentials.authorize(Http())

by this one :

import httplib2shim
http_auth = credentials.authorize(httplib2shim.Http())
Antoine Tissier
  • 621
  • 5
  • 13
  • Dear Antonie, I have done what you said in [this post](https://stackoverflow.com/questions/65873845/setting-proxy-for-google-analytics-reporting-api). Unfortunately it doesn't work. Would you mind taking look at that? – Mostafa Ghadimi Jan 25 '21 at 09:50
1

I decided to recode my web app in Python 2, still using the httplib2 package. Proxy info are now taken into account. It now works.

Phil27
  • 233
  • 1
  • 3
  • 12
  • could you describe your solution more precisely please? – Liso Aug 10 '16 at 18:39
  • 2
    Indeed! I have a script which fails with python 3.4, but runs fine with python 2.7. And the only difference in behavior seems to be that with pyhon 3.4 it tries to connect to Google directly, while under 2.7 it correctly goes through the proxy. – chutz Aug 18 '16 at 16:29
  • absolutly - @Liso the proxy info is not taken into account in Python 3.* but in Python 2.* – Phil27 Nov 23 '16 at 16:08
  • 1
    Did anyone solve it for python 3? I need to connect to google drive api through a proxy with python 3. – elvainch Feb 14 '18 at 10:59