Question

I am walking through the steps in chapter one of Mining the Social Web 2nd Edition and keep receiving an error when calling Yahoo GeoPlanet's WOE ID system to retrieve trends. OAuth info has been redacted with '*'. Github repository wasn't much help.

As a side note, I ended up having to use

from twitter import Twitter

Instead of the code on GitHub. Not sure why, but import Twitter (and import twitter) were not working for me. Went through pip install, but still had to change the library call.

Initial Authentication seems to work fine:

from twitter import Twitter
consumer_key = '*****'
consumer_secret = '*****'
oauth_token = '*****'
oauth_secret = '*****'

auth = twitter.oauth.OAuth(oauth_token, oauth_secret, consumer_key, consumer_secret)

twitter_api = twitter.Twitter(auth=auth)

print twitter_api

<twitter.api.Twitter object at 0x106******>

But the attempt to call the API fails:

WORLD_WOE_ID = 1
US_WOE_ID = 23424977

world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)

print world_trends
print
print us_trends

TwitterHTTPError                          Traceback (most recent call last)
<ipython-input-29-e2835459a5aa> in <module>()
     10 # to the URL itself as a special case keyword argument.
     11 
---> 12 world_trends = twitter_api.Twitter.trends.place(_id=WORLD_WOE_ID)
     13 us_trends = twitter_api.Twitter.trends.place(_id=US_WOE_ID)
     14 

/Users/User/anaconda/lib/python2.7/site-packages/twitter/api.pyc in __call__(self, **kwargs)
    243 
    244         req = urllib_request.Request(uriBase, body, headers)
--> 245         return self._handle_response(req, uri, arg_data, _timeout)
    246 
    247     def _handle_response(self, req, uri, arg_data, _timeout=None):

/Users/User/anaconda/lib/python2.7/site-packages/twitter/api.pyc in _handle_response(self, req, uri, arg_data, _timeout)
    274                 return []
    275             else:
--> 276                 raise TwitterHTTPError(e, uri, self.format, arg_data)
    277 
    278 

TwitterHTTPError: Twitter sent status 401 for URL: 1.1/Twitter/trends/place.json using parameters: 
(id=1&oauth_consumer_key=*****&oauth_nonce=*****&oauth_signature_method=HMAC-SHA1&oauth_timestamp=*****&oauth_token=*****&oauth_version=1.0&oauth_signature=*****)
details:{"errors":[{"message":"Could not authenticate you","code":32}]}

Why am I able to authenticate successfully, but unable to make the call?

Was it helpful?

Solution 2

User error. Typical. Instead of actually following the instructions in the book, I went off the rails a bit installing IPython Notebook manually and trying to run things on my own. If you are reading through this book, make sure you follow these (and all) steps very carefully:

  1. Install VirtualBox
  2. Install Vagrant
  3. Copy Mining the Social Web Second Edition's GitHub repository
  4. Clone to your computer using git init {copied git address}
  5. 'Vagrant Up' the book's folder (this can take a while)
  6. Open IP[y] Notebook through http://localhost:8888
  7. Go into Chapter 1 - Mining Twitter
  8. Use the code provided by the book's author (Matthew A. Russell)
  9. Retrieve Twitter API keys
  10. Plug and play

Now, why I wasn't able to manually input this data, I am still not entirely sure. However, it is likely the use of IP[y] Notebook required a very specific install process to ensure ipython and its corresponding libraries were properly installed and available. This seems particularly likely because I am no longer having problems calling the 'twitter' library/package.

Link to the Mining the Social Web, Second Edition's
GitHub repository: https://github.com/ptwobrussell/Mining-the-Social-Web-2nd-Edition
Vimeo channel (VERY helpful): http://vimeo.com/channels/MiningTheSocialWeb

Hope this helps someone else.

OTHER TIPS

Instantiating the Twitter object doesn't authenticate you; it just stores the credentials until you actually try to make an API call with them. Authentication is not attempted until you receive the error.

Double-check your OAuth credentials are correct and make sure your system date and time are set correctly, since it can cause handshaking problems if the time is off by a lot. You can regenerate an OAuth token for your account by logging in at https://apps.twitter.com on your app's API Keys page under the "Your access token" header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top