request.get(url)在python中不起作用

时间:2013-12-10 09:55:44

标签: python urllib

我正在尝试通过request.get(url)方法访问网址。它显示错误:

raise ConnectionError(e)
requests.exceptions.ConnectionError:
HTTPSConnectionPool(host='jawbone.com', port=443):
Max retries exceeded with url: /auth/oauth2/token?&client_id=ln1Fmh0-_fM&client_secret=6347a817372fe10c8168a6b2a08cdb6d2c0eb359&grant_type=authorization_code&code=aV1SI82xvTorFfSJUj4SV2na6cgo2_v-uVoO6syutrvyigmX5T19dxgi1Notq2BX3S9lsTYELynpGgsuWgbWr--GVkL4W468gA0vox9BEJVeQaE-y_Ag1qIwg5Jnf2wau806cG3fyeIbb4kkn8mrrqgZaLdBfBAfXaCH7rMkJ9avUszB10Lbxx5Ft_tbuFx2pe4N3piQM1xAKMXNVOTe6nt573H32ynarIFYoy5Otuw
(Caused by <class 'TimeoutError'>: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)

当我尝试使用webrowser(url)访问相同的URL时,它工作正常,但我无法获取响应对象。

代码如下

def getAccessToken(self, code):
     u = 'jawbone.com/auth/oauth2/token?';
     u += '&client_id=' + self.client_id 
     u += '&client_secret=' + self.client_secret 
     u += '&grant_type=authorization_code' 
     u += '&code=' + code 
     print(u) 
     res = requests.get(u) 
     if res.status_code == 200: 
         print('success') 
        return json.loads(res) 
     else:
        return res 

2 个答案:

答案 0 :(得分:0)

我在浏览器中尝试过您的请求,其显示的内容是:

  

{   ERROR_DESCRIPTION: “验证码已过期:aV1SI82xvTorFfSJUj4SV2na6cgo2_v-uVoO6syutrvyigmX5T19dxgi1Notq2BX3S9lsTYELynpGgsuWgbWr - GVkL4W468gA0vox9BEJVeQaE-y_Ag1qIwg5Jnf2wau806cG3fyeIbb4kkn8mrrqgZaLdBfBAfXaCH7rMkJ9avUszB10Lbxx5Ft_tbuFx2pe4N3piQM1xAKMXNVOTe6nt573H32ynarIFYoy5Otuw”   错误:“invalid_request”   }

然后我使用requests.get()发出请求:

#!/usr/bin/env python2.7
#coding: utf-8

import requests

url = 'https://jawbone.com/auth/oauth2/token?/auth/oauth2/token?&client_id=ln1Fmh0-_fM&client_secret=6347a817372fe10c8168a6b2a08cdb6d2c0eb359&grant_type=authorization_code&code=aV1SI82xvTorFfSJUj4SV2na6cgo2_v-uVoO6syutrvyigmX5T19dxgi1Notq2BX3S9lsTYELynpGgsuWgbWr--GVkL4W468gA0vox9BEJVeQaE-y_Ag1qIwg5Jnf2wau806cG3fyeIbb4kkn8mrrqgZaLdBfBAfXaCH7rMkJ9avUszB10Lbxx5Ft_tbuFx2pe4N3piQM1xAKMXNVOTe6nt573H32ynarIFYoy5Otuw'

res = requests.get(url)
print res.content

它打印的内容与浏览器显示的内容相同。没有什么不对的。

答案 1 :(得分:0)

您超出了速率限制(您向服务器发送了太多请求)。 在达到限制之前的几秒钟内确定您被分配和睡眠的最大速率(time.sleep(x)