Skip to content

Instantly share code, notes, and snippets.

@blech
Last active August 29, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blech/43573 to your computer and use it in GitHub Desktop.
Save blech/43573 to your computer and use it in GitHub Desktop.
Old Flickr auth example in Perl
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use DBI;
use Flickr::API;
use XML::LibXML;
use Compress::Zlib; # boggle
# flickr initialisation
my $key = undef;
my $secret = undef; # your key and secret here
my $api = new Flickr::API({'key' => $key, 'secret' => $secret});
my $auth_token = '' || get_auth_token(); # get_auth_token lifted from flickr-export
my $nsid = ''; # put in your NSID here (eg '48600109393@N01')
## THE ACTUAL TAGGING BIT IS HERE
my $photo_id = "310301300313";
my $rsp = get_flickr_data('flickr.photos.addTags',
{ 'photo_id' => $photo_id,
'tags' => '"tags go here", "and here"', });
## and this is the auth cruft
sub get_auth_token {
# The user wants to authenticate. There's really no nice way to handle this.
# So we have to spit out a URL, then hang around or something until
# the user hits enter, then exchange the frob for a token, then tell the user what
# the token is and hope they care enough to stick it into .flickrrc so they
# only have to go through this crap once.
# 1. get a frob
my $frob = get_frob( $api );
# 2. get a url for the frob
my $url = $api->request_auth_url('write', $frob);
# 3. tell the user what to do with it
print "1. Enter the following URL into your browser\n\n",
"$url\n\n",
"2. Follow the instructions on the web page\n",
"3. Hit <Enter> when finished.\n\n";
# 4. wait for enter.
<STDIN>;
# 5. Get the token from the frob
my $auth_token = get_token( $api, $frob );
die "Failed to get authentication token!" unless defined $auth_token;
# 6. Tell the user what they won.
print "Your authentication token for this application is\n\t\t", $auth_token, "\n";
print "Edit this program and insert this at line 20\n";
exit 0;
}
sub get_frob {
my $api = shift;
my $res = $api->execute_method("flickr.auth.get_frob");
return undef unless defined $res and $res->{success};
# FIXME: error checking, please. At least look for the node named 'frob'.
return $res->{tree}->{children}->[1]->{children}->[0]->{content};
}
sub get_token {
my $api = shift;
my $frob = shift;
my $res = $api->execute_method("flickr.auth.get_token",
{ 'frob' => $frob } );
return undef unless defined $res and $res->{success};
# FIXME: error checking, please.
return $res->{tree}->{children}->[1]->{children}->[1]->{children}->[0]->{content};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment