I’ve got a bit sidetracked recently while trying to convert my blog from using Typo to a static site using Webby. Typo provides a facility to tag your blog posts. This allows visitors to find all the articles tagged e.g. mocha.
Generating these tag pages becomes more awkward with the Webby-based site which is generated at deploy-time. I decided I liked the approach that Chris Roos took which was to tag his blog posts in del.icio.us so that visitors can use a del.icio.us URL to find all the articles with a particular tag.
Initially I used WWW::Delicious which worked well enough with my main del.icio.us account. But then I decided I didn’t want to clutter up this account with bookmarks to all my blog posts, so I created a new account. Unfortunately since this is a new account, I have to use the v2 API with OAuth if I want to programmatically add bookmarks :-
To access data from accounts created using a Yahoo! ID, use the same API’s as below, but change the path to /v2, and make HTTP requests using OAuth as provided by the Yahoo! Developer Network.
I couldn’t find any examples of anyone doing this using Ruby, so I decided to have a go using the OAuth gem. It was a bit of a painful process due to a lack of decent documentation, but I got there in the end. I thought I’d share the code in case it saves anyone else a bit of time.
The code probably isn’t very robust since it’s lacking tests and it currently only offers the ability to add bookmarks which was all I needed. Probably what I should do next is integrate this code with something like WWW:Delicious to offer access to both v1 and v2 APIs, but since I can now do what I wanted, this is unlikely to happen in the near future.
In order to get the example code to work, you’ll need to login to a Yahoo! ID based del.icio.us account and go to your My Projects page :-
There you need to add a project and grant it read/write access to del.icio.us :-
Granting access to your project will generate a Consumer Key and a Consumer Secret. These values should be used to define the
SHARED_SECRET constants respectively. The
example.rb code does this by requiring a
constants.rb file which you could add.
Here’s the example code which creates a single bookmark. Note that the first time this is run, it will open a browser and require you to login to obtain an
oauth_verifier code :-
You will need to copy and paste this code into the terminal window :-
All the relevant tokens are saved to YAML files, so that subsequent calls to the API will work without user interaction. However, you should note that an access token expires after 1 hour. It ought to be possible to refresh this token without user interaction, but I haven’t been able to get that working yet, so in this case you’ll probably need to delete
access_token.yml and re-run the script.
$LOAD_PATH << File.join(File.dirname(__FILE__), 'oauth-extensions') $LOAD_PATH << File.join(File.dirname(__FILE__), 'delicious')
require 'delicious/api' require 'constants'
api = Delicious::API.new(API_KEY, SHARED_SECRET) api.posts_add!( :url => 'http://www.google.com/', :description => 'Testing 1 2 3', :extended => 'Blah blah blah', :tags => 'testing google blah' )
Finally, here’s the bookmark successfully added to my account :-