[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 |
|
---|
[32950] | 50 | # read_file()
|
---|
| 51 | # Derived from:
|
---|
| 52 | # https://stackoverflow.com/questions/953707/in-perl-how-can-i-read-an-entire-file-into-a-string
|
---|
| 53 | sub read_file
|
---|
| 54 | {
|
---|
| 55 | my ($file) = @_;
|
---|
| 56 |
|
---|
| 57 | my $text = do {
|
---|
| 58 | local $/ = undef;
|
---|
| 59 | open my $fh, "<", $file
|
---|
| 60 | || die "could not open $file: $!";
|
---|
| 61 | <$fh>;
|
---|
| 62 | };
|
---|
[32931] | 63 |
|
---|
[32950] | 64 | chomp($text);
|
---|
| 65 | return $text;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | my $DEBUG=1;
|
---|
| 69 | my $WEB_DISPLAY=0;
|
---|
| 70 |
|
---|
[32931] | 71 | my $access_key = 'OAUTH_CONSUMER_KEY'; # replace with your access_key
|
---|
| 72 | my $secret_key = 'OAUTH_CONSUMER_SECRET'; # replace with your secret_key
|
---|
| 73 | my $ip_address = 'MY_IP_ADDRESS'; # replace with the IP address of your workstation or server
|
---|
[32950] | 74 |
|
---|
| 75 | if (-f "my_access_key.txt") {
|
---|
| 76 | $access_key = read_file("my_access_key.txt");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if (-f "my_secret_key.txt") {
|
---|
| 80 | $secret_key = read_file("my_secret_key.txt");
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | if (-f "my_ip_address.txt") {
|
---|
| 84 | $ip_address = read_file("my_ip_address.txt");
|
---|
| 85 | }
|
---|
[32931] | 86 |
|
---|
[32950] | 87 | my $base_request_url = "https://babel.hathitrust.org/cgi/htd/volume/pageimage/";
|
---|
| 88 |
|
---|
| 89 | #my $request_url = $base_request_url."mdp.39015000000128/1";
|
---|
| 90 | my $request_url = $base_request_url."mdp.39015070661841/1";
|
---|
| 91 |
|
---|
[32931] | 92 |
|
---|
| 93 | my $consumer = OAuth::Lite::Consumer->new
|
---|
| 94 | (
|
---|
| 95 | consumer_key => $access_key,
|
---|
| 96 | consumer_secret => $secret_key,
|
---|
| 97 | auth_method => OAuth::Lite::AuthMethod::URL_QUERY,
|
---|
| 98 | );
|
---|
| 99 |
|
---|
[32950] | 100 | if ($DEBUG) {
|
---|
| 101 | my $params = $consumer->gen_auth_params('GET', $request_url);
|
---|
| 102 | print STDERR "(Info) Resolved URL for retrieval:\n";
|
---|
| 103 | print STDERR "$request_url?v=2&";
|
---|
| 104 | print STDERR "oauth_consumer_key=$params->{'oauth_consumer_key'}&";
|
---|
| 105 | print STDERR "oauth_timestamp=$params->{'oauth_timestamp'}&";
|
---|
| 106 | print STDERR "oauth_nonce=$params->{'oauth_nonce'}&";
|
---|
| 107 | print STDERR "oauth_signature_method=$params->{'oauth_signature_method'}&";
|
---|
| 108 | print STDERR "oauth_signature=$params->{'oauth_signature'}&";
|
---|
| 109 | print STDERR "oauth_version=$params->{'oauth_version'}\n";
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[32931] | 112 | my $response = $consumer->request
|
---|
| 113 | (
|
---|
| 114 | method => 'GET',
|
---|
| 115 | url => $request_url,
|
---|
| 116 | params => {
|
---|
| 117 | v => '2',
|
---|
| 118 | format => 'png',
|
---|
| 119 | ip => $ip_address,
|
---|
| 120 | },
|
---|
| 121 | );
|
---|
| 122 |
|
---|
| 123 | my $success = $response->is_success;
|
---|
| 124 | my $content_type = 'image/png';
|
---|
| 125 |
|
---|
| 126 | unless ($success) {
|
---|
| 127 | $content_type = 'text/html';
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[32950] | 130 | if ($WEB_DISPLAY) {
|
---|
| 131 | print CGI::header(
|
---|
| 132 | Content_type => $content_type,
|
---|
| 133 | Status => $response->status_line,
|
---|
| 134 | );
|
---|
[32931] | 135 |
|
---|
[32950] | 136 | unless ($success) {
|
---|
| 137 | print "<p><b>Error: " . $response->content . "</b><br/>";
|
---|
| 138 | }
|
---|
| 139 | else {
|
---|
| 140 | print $response->content;
|
---|
| 141 | }
|
---|
[32931] | 142 | }
|
---|
| 143 | else {
|
---|
| 144 | print $response->content;
|
---|
| 145 | }
|
---|
| 146 | exit $success;
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 |
|
---|