1

I built a Python 3.4 Web Application calling Google Analytics API.

class GA:

    def __init__(self):
        self.scope = ['https://www.googleapis.com/auth/analytics.readonly']
        self.service_account_email = 'my_account_email'
        self.key_file_location = 'my_key_location'
        self.ga_id = 'my_ga_id'


    def get_service(self, api_name = 'analytics', api_version = 'v3'):
        f = open(self.key_file_location, 'rb')
        key = f.read()
        f.close()
        credentials = SignedJwtAssertionCredentials(self.service_account_email, key,scope=self.scope)

        http = credentials.authorize(httplib2.Http())

        service = build(api_name, api_version, http=http)
        self.service = service
        return (service)

 ga = GA()
 ga.get_service()

It works perfectly without proxy

but I need to set it up on a windows server running behind a corporate proxy. So I tried to replace the http object by :

p = httplib2.proxy_info_from_url("http://username:pwd@myproxyname:80")
http = credentials.authorize(httplib2.Http(proxy_info=p))

But it doesn't work. So I also tried with :

os.environ['HTTP_PROXY']="http://username:pwd@myproxyname:80"
p = httplib2.proxy_info_from_environment(method='http')
http = credentials.authorize(httplib2.Http(proxy_info=p))

But it is not working either. I checked all the related questions without success. I always get a TimeoutError: [WinError 10060]

Phil27
  • 223
  • 1
  • 3
  • 10

3 Answers3

0

For those who are interesting, I decided to recode my web app in Python 2 ; still using httplib2 package;

The same code works (proxy info are taken into account contrary to Python 3)

Phil27
  • 223
  • 1
  • 3
  • 10
0

I've just come across this question and it's relevant to a solution I've implemented in Python 3.6.

I tried a number of different methods but the only one that httplib2 seems to recognize the proxy is through modifying the http_proxy environment variable

I've also had to remove SSL validation since that was the only way I could get this working. I wouldn't recommend this approach, unless you trust the domain you're requesting from won't be compromised.

os.environ["https_proxy"] = "https://PROXY_URL:PROXY_PORT"
http = credentials.authorize(http=httplib2.Http(disable_ssl_certificate_validation=True))

This approach also works for the initial request to the authorization/discovery URL's, but fails later on if you need to request information from the API. I was above to dig into the google analytics api source code before I found a much simpler solution (above)

# http = credentials.authorize(http=httplib2.Http(proxy_info=httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, "PROXY_URL", PROXY_PORT), disable_ssl_certificate_validation=True))
huberu
  • 35
  • 6
0

huberu, your code worked fine, you helped me a lot!
Blessings for you and your family!

If someone needs to test, you can use the code bellow to simulate a proxy connection:

import httplib2
import os

os.environ['https_proxy'] = '127.0.0.1:80'
http = httplib2.Http(disable_ssl_certificate_validation=True)

resp, content = http.request('http://google.com', 'GET')
print(resp)
print(content)
Parx
  • 1
  • 1