Showing posts with label Twitter. Show all posts
Showing posts with label Twitter. Show all posts

Monday, September 27, 2010

Twitter oAuth SHA1 Digest Class / Webmatrix Helper

*** Update*** For some reason the class wasn't showing up, making this info useless. Fixed.

I've decided to use Twitter's @anywhere Javascript service to integrate Twitter functionality into a website I'm building.

I found it pretty easy to use. I decided to integrate some server side logic once a person logged into my website using Twitter. Knowing that Javascript is pretty easy to spoof, I wanted to explore ways of not taking a given user's Twitter login for granted. I found this cryptic paragraph in the @anywhere documentation listed above.

Once the user has authorized the host site, Anywhere will set a cookie named "twitter_anywhere_identity" that contains the id of the logged in user. You can read this on the server side to learn the user's ID. The format of the cookie is:

user_id:signature

When reading the cookie on the server, you should use the signature to verify that this information has come from Twitter. Calculate the signature by appending the given user_id to your OAuth consumer secret and creating a SHA1 hex digest. If this matches the signature in the identity cookie the user ID is verified. For example, in Ruby:

Digest::SHA1.hexdigest(user_id + consumer_secret)

Ut Oh. I know Twitter tends to not have a lot of examples in .Net floating around. After Googling for a bit I found this excellent article on .Net and SHA1 Interop by Jonathan Cogley-

http://authors.aspalliance.com/thycotic/articles/view.aspx?id=2

After doing a few minor tweeks to Jonathan's base code (name space change from .Net 1.1), I present to you a Twitter oAuth SHA1 Digest class




Usage-

The above class will work with any flavor of .Net. If you want to use the above class as a Webmatrix helper, create a class file, rename it TweetSHADigest.cs, paste over the generated code in the file with the above code. The class file should be in a folder called App_Code.

Once Twitter has made a call back to your web site, you can verify that the cookie set came from twitter like so. You will need your web application's Twitter OAuth consumer secret.




Saturday, September 25, 2010

Simple Twitter @anywhere Login/Logout Custom Button

Here is a simple Twitter @anywhere login logout button jQuery script. Note this assumes you have added your a reference to jQuery and your Twitter anywhere javascripts to your page, as well as having a span somewhere on your page with the id of "lbtn".




This is just to get you started, change and configure as you like.

Thursday, August 26, 2010

Using Twitter's @anywhere Service on Localhost (and with Web Matrix/IIS Express)

Here is the trick to get Twitter's @anywhere service (http://dev.twitter.com/anywhere) working on your localhost website for development and testing.

1) Don't put a call back url when you register your app.
2) On the link below where you register the call back url for your app, click the add domain link.
3) Add either localhost, 127.0.0.1, or both (may not take 127.0.0.1)
4) In your javascript specify the callback url (localhost, 127.0.0.0/auth.cshtml, whatever. I didn't test this but I think you might even be able to enter ports in (localhost:8080). I ended up setting my web server to run on port 80 (default) so I didn't need to add any ports. If you can't enter ports, you might be stuck having to run your dev web server on port 80 or 8080.

Done. No editing hosts files, nada. Why is this so easy and not documented? I think it is because of a recent change on Twitter's end to loosen things up a bit so you can test your applications. For deployment, you may need to actually enter a call back url for your application. I don't thinks so, but test.

For me I'm working with Web Matrix, and I wanted iis express to run on Port 80. Three things.

1) Turn off/stop your default non-IIS Express web server (apache, dev version of IIS full) so you don't get any conflicts.
2) navigate to where the web matrix exe file is located by opening up a console by typing cmd in the start run menu, then navigate to the path by typing something like cd: c:\mypath\. Once there you can run iis express from the command line (only way right now to specify a port).

type something like iisexpress /path:c\mywebpath\mywebsite\ /port:80

3) Right now you can't have any spaces in the path name. I figure that will be fixed in the next beta or the final release.

Wednesday, August 18, 2010

Twitter

I love Twitter, I hate Twitter. If you don't get it, well, don't feel lame. Twitter has it's uses, but those uses are not for everyone. As an information gathering tool, a networking tool (as in meeting people with similar interest), and a marketing tool (or a pushing info/viewpoint tool...I'll put that under "marketing) Twitter is an awesome tool that will only get better.

I've had a few people look at my number of followers and ask how I and why I have the numbers that I do. My numbers aren't that impressive, nor does anyone's numbers really need to be high to use Twitter effectively. My numbers do look impressive to friends of mine that are just getting started with Twitter and wonder how to expand their following. I had so many questions about it I finally wrote something down for an indie gamer forum and then later posted that to associated content. The information might be of use to some, if so check it out.

A Simple Eight Point Strategy to Grow Your Twitter Following

Let me know if this helps, or please share any other successful strategies that you have used.