“TypeError: a float is required” occurred when using urllib2

牧云@^-^@ 提交于 2020-02-03 10:37:41

问题


I was following the twitter API documentation of issuing application-only requests. Error occurred when doing Step 2: Obtain a bearer token. I'm not familiar with the details of these modules in traceback, could anyone tell me how to make the code work?

Here is my code:

import urllib, urllib2, base64

consumer_key = 'KmdCpWGtKEtQ0EdZyQCEZGmcX'
consumer_secret = 'B3I0RcG8vsCC1Y4LBIHIlNvm2NrSB9smlPovYnKYjCyUoNBcMd'
consumer_key_secret = urllib.quote(consumer_key) + ':' + urllib.quote(consumer_secret)
token = base64.b64encode(consumer_key_secret)
headers = {'Authorization': 'Basic ' + token, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
data = {'grant_type': 'client_credenials'}

resp = urllib2.urlopen('https://api.twitter.com/oauth2/token', urllib.urlencode(data), headers)

Error:

Traceback (most recent call last):
  File "app_only_auth.py", line 12, in <module>
    resp = urllib2.urlopen('https://api.twitter.com/oauth2/token', urllib.urlencode(data), headers)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.7/httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 776, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 1157, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 559, in create_connection
    sock.settimeout(timeout)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
TypeError: a float is required

回答1:


The urlopen function uses this format: urllib2.urlopen(url[, data][, timeout])

In your code you are giving the headers to urlopen as the timeout value which causes you to get the TypeError.

The headers should be added by creating the request with the urllib2.Request() function. For example:

req = urllib2.Request('https://api.twitter.com/oauth2/token', urllib.urlencode(data), headers)
resp = urllib2.urlopen(req)


来源:https://stackoverflow.com/questions/23710894/typeerror-a-float-is-required-occurred-when-using-urllib2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!