Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeError: a float is required" occurred when using urllib2

Tags:

python

twitter

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
like image 806
Lee Avatar asked Jan 11 '23 14:01

Lee


1 Answers

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)
like image 182
Jani Avatar answered Feb 03 '23 12:02

Jani