Manual Twitter REST API Authentication
Basically I want to use the Twitter Users Lookup (REST API) method, this: http://apiwiki.twitter.com/w/page/24142947/Twitter-REST-API-Method:-users-lookup
but just for one request, so more less manually.
When the API supported basic authentication I could just plug in my username/password and gather information about the people tweeting on a given subject/search (as to how many followers they have, location, etc - mainly for reach/ROI purposes). I would just request the XML, format it and shove it into an excel file to play with.
Since Twitter no开发者_运维技巧 longer supports basic authentication with this method is there another way I can manually request this information, or do I actually need to setup oAuth?
Is there another option that's easier?
I use LINQPad for all of my Twitter spelunking; it's simply one of the best tools I've used for exercising any code. It's a free utility (you only have to pay if you want statement autocompletion and some other nifty features, but you don't need them). If you don't run Windows then this won't be of much use to you, but if you have a Windows box you can use, then the ability to use the Dump()
method provide by LINQPad is just a massive timesaver.
Using LINQPad
First you have to register an application with Twitter. Adam has a very nice writeup on setting up your application on dev.twitter.com in his answer.
Once you have your OAuth tokens, download and install LINQPad and the .NET Framework 4.0 (if you don't already have it.)
Next grab Twitterizer, which is a great .NET Twitter library. I'm using version 2.3.1 for this example. Unpack the ZIP file to a location you can reference later. Now we can get started.
Start LINQPad, click on the Query 1 window, and change the Language to C# Statements.
Next press F4 to open the Query Properties. On the Additional References tab click Browse... and find Twitterizer2.dll
where you extracted it earlier.
Now, click on the Additional Namespace Imports tab and type Twitterizer
into the window like so:
Now click OK, and we can write our query.
In the Query 1 window, enter the following code:
OAuthTokens tokens = new OAuthTokens();
tokens.ConsumerKey = "YourConsumerKey";
tokens.ConsumerSecret = "YourConsumerSecret";
tokens.AccessToken = "YourAccessToken";
tokens.AccessTokenSecret = "YourAccessSecret";
TwitterUser.Lookup(
tokens,
new LookupUsersOptions {
ScreenNames={"arcain","dotnetdevbuzz"}, IncludeEntities=true
}
).Dump(); // the magic happens here!
Now hit F5 to execute the query, and off LINQPad goes to Twitter to fetch your results.
The results using Dump()
are nicely formatted, and the entire object is rendered without having to reference anything explicitly, like so:
You can then click Export Results to export to Excel, Word, or just HTML, although you may want to reference some of the object fields directly to target your report data.
Oh, and you can apply Dump()
to just about anything, so it is a nice addition to any toolbox. Anyway, I hope you can make use of this as I find it a real time saver.
I finished the above, and then remembered the Twitter dev console, Twurl. Twurl is a bare-bones console that is available from the Apps tab on dev.twitter.com. It can be found by following a link on the right side of the page:
Now, Twurl would be great if it wasn't broken, but it still is. So, the next best thing (if you still want a webby console alternative to LINQPad) would be using a free service like apigee.com which supports OAuth as well.
If you don't want to do any programming and just enter a URL in a browser, you can do this one user at a time with /users/show:
http://api.twitter.com/1/users/show.xml?screen_name=barackobama
This doesn't require any authentication. If you want to use /users/lookup to get multiple users at one time, you do have to write code that uses OAuth.
精彩评论