Python Twitter API trying to retrieve tweet but error: AttributeError: 'int' object has no attribute 'encode'
<p>why am I getting an AttributeError: 'int' object has no attribute 'encode'?
I am trying to retrieve a tweet using the Twitter API on Python. Full traceback here: </p>

<pre><code>Traceback (most recent call last):
  File "C:/Python27/lol.py", line 34, in &lt;module&gt;
    headers = req.to_header()
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 398, in to_header
    params_header = ', '.join(header_params)
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 397, in &lt;genexpr&gt;
    header_params = ('%s="%s"' % (k, v) for k, v in stringy_params)
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 396, in &lt;genexpr&gt;
    stringy_params = ((k, escape(v)) for k, v in oauth_params)
  File "build\bdist.win-amd64\egg\oauth2\__init__.py", line 163, in escape
    s = s.encode('utf-8')
AttributeError: 'int' object has no attribute 'encode'
</code></pre>

<p>Below is the code I'm using.</p>

<pre><code>import oauth2
import time
import urllib2
import json
url1="https://api.twitter.com/1.1/search/tweets.json"
params = {
    "oauth_version": "1.9.0",
    "oauth_nonce": oauth2.generate_nonce(),
    "oauth_timestamp": int(time.time())
    }

consumer = oauth2.Consumer(key="*********", secret="*********")
token = oauth2.Token(key="*********", secret="*********")

params["oauth_consumer_key"] = consumer.key
params["oauth_token"] = token.key

for i in range(1):
    url = url1
    req = oauth2.Request(method="GET", url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, token)
    headers = req.to_url()
    print headers
    print url

for i in range(1):
    url = url1
    params["q"] = "pictorial"
    params["count"] = 2
    req = oauth2.Request(method="GET", url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, token)
    headers = req.to_header()
    url = req.to_url()
    response = urllib2.Request(url)
    data = json.load(urllib2.urlopen(response))
    if data["statuses"] == []:
        print "end of data"
        break
    else:
        print data
</code></pre>

<p>And if I change int(time.time()) into str(time.time())
I get the following error:</p>

<pre><code>Traceback (most recent call last):
  File "C:/Python27/lol.py", line 37, in &lt;module&gt;
    data = json.load(urllib2.urlopen(response))
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 437, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 400: Bad Request
</code></pre>

<pre><code>"oauth_timestamp": int(time.time())
</code></pre>

<p>here you use an <code>int</code>, but that field <em>must</em> be a string.</p>

python api twitter