[32931] | 1 | #!/usr/bin/env perl
|
---|
| 2 |
|
---|
| 3 | =head1 NAME
|
---|
| 4 |
|
---|
| 5 | dapiclient_2
|
---|
| 6 |
|
---|
| 7 | =head1 DESCRIPTION
|
---|
| 8 |
|
---|
| 9 | This is an example perl 2-legged oauth client that makes a request to
|
---|
| 10 | the full Data API. Replace OAUTH_CONSUMER_SECRET and
|
---|
| 11 | OAUTH_CONSUMER_KEY (below) with the keys you received from
|
---|
| 12 | http://babel.hathitrust.org/cgi/kgs and MY_IP_ADDRESS by the IP
|
---|
| 13 | address of workstation or server running this script.
|
---|
| 14 |
|
---|
| 15 | It uses a hardcoded URL to request a single page image for a public domain item.
|
---|
| 16 |
|
---|
| 17 | It is intended to aid development of a fully function Data API client
|
---|
| 18 | in Perl or other languages that implement HMAC_SHA1 OAuth libraries.
|
---|
| 19 |
|
---|
| 20 | =head1 SYNOPSIS
|
---|
| 21 |
|
---|
| 22 | At the Unix command line:
|
---|
| 23 |
|
---|
| 24 | % cd /path/to/dapiclient_2/directory
|
---|
| 25 | % chmod u+x dapiclient_2
|
---|
| 26 |
|
---|
| 27 | then:
|
---|
| 28 |
|
---|
| 29 | % curl 'http://yourhost/path_to_client/dapiclient_2' > mdp.39015000000128.png
|
---|
| 30 |
|
---|
| 31 | If your png is corrupt, run without i/o redirection to see the error code:
|
---|
| 32 |
|
---|
| 33 | % curl 'http://yourhost/path_to_client/dapiclient_2'
|
---|
| 34 |
|
---|
| 35 | or paste the URL to your browser.
|
---|
| 36 |
|
---|
| 37 | =head1 OUTPUT
|
---|
| 38 |
|
---|
| 39 | Stream of data for the above mentioned page image file or error code.
|
---|
| 40 |
|
---|
| 41 | =cut
|
---|
| 42 |
|
---|
| 43 | use strict;
|
---|
| 44 | use warnings;
|
---|
| 45 |
|
---|
| 46 | use CGI;
|
---|
| 47 | use OAuth::Lite::Consumer;
|
---|
| 48 | use OAuth::Lite::AuthMethod;
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | my $access_key = 'OAUTH_CONSUMER_KEY'; # replace with your access_key
|
---|
| 52 | my $secret_key = 'OAUTH_CONSUMER_SECRET'; # replace with your secret_key
|
---|
| 53 | my $ip_address = 'MY_IP_ADDRESS'; # replace with the IP address of your workstation or server
|
---|
| 54 |
|
---|
| 55 | my $request_url = 'https://babel.hathitrust.org/cgi/htd/volume/pageimage/mdp.39015000000128/1';
|
---|
| 56 |
|
---|
| 57 | my $consumer = OAuth::Lite::Consumer->new
|
---|
| 58 | (
|
---|
| 59 | consumer_key => $access_key,
|
---|
| 60 | consumer_secret => $secret_key,
|
---|
| 61 | auth_method => OAuth::Lite::AuthMethod::URL_QUERY,
|
---|
| 62 | );
|
---|
| 63 |
|
---|
| 64 | my $response = $consumer->request
|
---|
| 65 | (
|
---|
| 66 | method => 'GET',
|
---|
| 67 | url => $request_url,
|
---|
| 68 | params => {
|
---|
| 69 | v => '2',
|
---|
| 70 | format => 'png',
|
---|
| 71 | ip => $ip_address,
|
---|
| 72 | },
|
---|
| 73 | );
|
---|
| 74 |
|
---|
| 75 | my $success = $response->is_success;
|
---|
| 76 | my $content_type = 'image/png';
|
---|
| 77 |
|
---|
| 78 | unless ($success) {
|
---|
| 79 | $content_type = 'text/html';
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | print CGI::header(
|
---|
| 83 | Content_type => $content_type,
|
---|
| 84 | Status => $response->status_line,
|
---|
| 85 | );
|
---|
| 86 |
|
---|
| 87 | unless ($success) {
|
---|
| 88 | print "<p><b>Error: " . $response->content . "</b><br/>";
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
| 91 | print $response->content;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | exit $success;
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 |
|
---|