OAuth 1.0 with Perl

TL;DR

Accessing some services might require your client to support OAuth 1.0, the Perl module WWW::OAuth can help you. The interesting thing with this module is that it’s portable!

I don’t really know much about OAuth 1.0, and I can live with that. One thing I know is that WWW::OAuth can be set to handle this protocol automatically with Mojo::UserAgent, as can be seen in the documentation:

my $ua = Mojo::UserAgent->new;
$ua->on(start => sub { $oauth->authenticate($_[1]->req) });

This is a simple program to download a random SVG image from The Noun Project, via their API.

Local version here.

The term to be searched can be passed as the first command-line option. You are supposed to put your credentials in a JSON-formatted file, by default noun-project.json (although you can pass the file path on the command line as a second parameter).

The constructor (lines 18-21) is just the plain one from the documentation. Then it is set as an automatic handler in the user agent (lines 23 and 24), so that every request will be handled automatically. This makes this user agent specialized for the specific API endpoint, which is fine in our case. Otherwise… you can always create a new agent instance 😉

The rest is just plain Mojo::UserAgent usage: get the right API endpoint (we’re looking for the specific $term among the public domain icons), select one icon_url randomly inside and download it locally. Easy!


Comments? Octodon, , GitHub, Reddit, or drop me a line!