Python script that gets my latest tweet stopped working on my server
Question: Consider the following code:

    
    
    import twitter
    api = twitter.Api()
    most_recent_status = api.GetUserTimeline('nemesisdesign')[0].text
    

On my server (nemesisdesign.net) stopped working a few days ago.

If I try the same code from my own machine it works fine.

This is the stack trace:

    
    
    >>> most_recent_status = api.GetUserTimeline('nemesisdesign')[0].text
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "build/bdist.linux-i686/egg/twitter.py", line 1414, in GetUserTimeline
        json = self._FetchUrl(url, parameters=parameters)
      File "build/bdist.linux-i686/egg/twitter.py", line 2032, in _FetchUrl
        url_data = opener.open(url, encoded_post_data).read()
      File "/usr/local/lib/python2.6/urllib2.py", line 397, in open
        response = meth(req, response)
      File "/usr/local/lib/python2.6/urllib2.py", line 510, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/local/lib/python2.6/urllib2.py", line 435, in error
        return self._call_chain(*args)
      File "/usr/local/lib/python2.6/urllib2.py", line 369, in _call_chain
        result = func(*args)
      File "/usr/local/lib/python2.6/urllib2.py", line 518, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    HTTPError: HTTP Error 404: Not Found
    

Any hint? I have no clue ... :-(


Answer: First 3 general points:

  1. Twitter deprecated many portions of their API as Paul noted. You should check to see if you're using a currently supported mechanism.

  2. The official twitter python docs are beyond bad. Last I checked, they did not reflect any current libraries or connection protocols. Use them as a last resort. 

  3. The twitter recommended python libraries are all grossly out of date. Many don't work at all, most don't work against the current API.

I'd suggest you switch libraries to Twython. It's actively maintained and
supports the current API. There are a handful of other actively maintained
twitter libraries too. If something has been patched in 2 months though - find
another library.

Looking at your exact code:

  1. Your API object isn't passing in any credentials. IIRC, This was allowed in the 1.0 API , but the 1.1 API disallows this - you must authenticate with API credentials now :

     * <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline> (current)
     * <https://dev.twitter.com/docs/api/1/get/statuses/user_timeline> (past)
  2. You should try to inspect that object and find out the URL that created the 404. I'm guessing it as a 1.0 endpoint. you might be using a newer version of the "twitter" package ( whatever that is , there are multiple ones in that namespace ) locally, but a different one on the server. you should check what versions are on each machine. 

  3. Your code should trap the HTTP errors. Twitter's API is nice enough to use HTTP status codes are part of the error messaging ( <https://dev.twitter.com/docs/error-codes-responses> ) and will also send info on the headers. This can ofter tell you exactly what is going on. Libraries like Twython do most of this for you. 

Your code as-presented couldn't have caused that error though -- so i'm at a
loss as to what is going on.

Not passing in credentials should generate a 400; bad credentials a 401. Rate
Limiting is 420 , 429. Blocking could be a 404 or 420.

If i had to guess though - locally you have a package that is going after the
1.1 API endpoint, but your server has a version of that package that is going
after 1.0 - which no longer exists.

