How do interact with the Twitter API using Mathematica? SocialMediaData is a lovely function, but appears to be severely limited in what it can draw from Twitter. For example, if I want to get a list of retweets of a Tweet I've sent there's an API call for this but not a means of doing so through SocialMediaData. How do I augment the function with all the API has to offer?


1 Answer 1


The Twitter API now requires Oauth 1.0a authentication, so Ragfield's Twitter.m won't work without modification. While I haven't solved the problem for Twitter's API, I have solved two similar authentication problems from Mathematica. You can see what I did here:

Can anyone explain how to use Mathematica's OAuth package?


I just tried it with Twitter and this seemed to work:

token = HTTPClient`OAuthAuthentication[
   "ConsumerKey" -> "your_api_key", 
   "ConsumerSecret" -> "your_api_secret", 
   "RequestEndpoint" -> "https://api.twitter.com/oauth/request_token",
   "AuthorizeEndpoint" -> "https://api.twitter.com/oauth/authorize", 
   "AccessEndpoint" -> "https://api.twitter.com/oauth/access_token"];
url = "https://api.twitter.com/1.1/statuses/retweets_of_me.json";
URLFetch[url, "OAuthAuthentication" -> token]

I don't really know for sure since I don't have any retweets.

  • $\begingroup$ Very nice. Is there a way to tweak this so that it doesn't request verification through a Twitter account. In particular, If I already know the access token and access token secret, is there a way to pass that in to HTTPClient`OAuthAuthentication? $\endgroup$
    – hailekofi
    May 3, 2013 at 15:33
  • $\begingroup$ Yes, here is what I use for fitbit, which will be the same for twitter: HTTPClient`OAuthToken[HTTPClient`OAuth`Private`OAuth10Parameters[ "api.fitbit.com", None, Automatic, "verifier", Automatic, Automatic, $FitbitConsumerKey, $FitbitConsumerSecret, "https://api.fitbit.com/oauth/request_token", "GET", "https://www.fitbit.com/oauth/authorize", "RemoveWhitespace", "https://api.fitbit.com/oauth/access_token", "GET", "HMAC-Sha1", "HMAC", "oob", "Text/1.0", "Text/1.0", None], HTTPClient`OAuth`Private`Token10[ $FitbitUserKey, $FitbitUserSecret]] $\endgroup$
    – Max
    May 6, 2013 at 2:05
  • $\begingroup$ OK... Wow. First, thank you! But how do you know what arguments to feed this function without documentation? $\endgroup$
    – hailekofi
    May 7, 2013 at 3:03
  • $\begingroup$ Is there a way to supply the oauth token and receive back not only the json contents but the header as well? Putting URLFetch[url,{"Content","Headers"}, "OAuthAuthentication" -> token] doesn't do the job... $\endgroup$
    – hailekofi
    Jun 4, 2013 at 17:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.