1

我構建了一個調用Google Analytics API的Python 3.4 Web應用程序。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() 

它完美無代理

,但我需要將其設置在後面跟着跑企業代理在Windows服務器上。所以我試圖通過替換http對象:

p = httplib2.proxy_info_from_url("http://username:[email protected]:80") 
http = credentials.authorize(httplib2.Http(proxy_info=p)) 

但它不起作用。所以我也試過:

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

但它不工作。我沒有成功地檢查了所有相關的問題。我總是得到一個TimeoutError:[WinError 10060]

+0

您是否已經驗證代理工作的其他網址/請求? – abraham

+0

確實。簡單的httplib2.Http(proxy_info = p).request('http://google.com') – Phil27

+0

也不起作用。請求包工作正常! 但我需要使用httplib2.Http()來適應谷歌服務對象 – Phil27

回答

0

對於那些有趣的人,我決定在Python 2中重新編碼我的web應用程序;仍然使用httplib2包;

相同的代碼工作(代理信息考慮在內相反到Python 3)