Leser: 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use strict; use Digest::HMAC_SHA1; use Encode qw(encode); use URI::Escape; use LWP::UserAgent; use HTTP::Request::Common ('POST'); my $api_url = "http://api.twitter.com/1/statuses/update.json"; my $status = "Hello world"; my $oauth_consumer_key = "XYZ"; my $oauth_consumer_secret = "XYZ"; my $oauth_nonce = "10000000001"; my $oauth_signature_method = "HMAC-SHA1"; my $oauth_token = "XYZ"; # from my application under my access token my $oauth_token_secret = "XYZ"; # from my application under my access token my $oauth_timestamp = "1272325550"; my $oauth_version = "1.0"; my $content = "oauth_consumer_key=$oauth_consumer_key&oauth_nonce= $oauth_nonce&oauth_signature_method= $oauth_signature_method&oauth_timestamp=$oauth_timestamp&oauth_token= $oauth_token&oauth_version=$oauth_version&status=$status"; my $signature_base_str = "POST&" . uri_escape_RFC3986($api_url) . "&" . uri_escape_RFC3986($content); my $HMAC_SHA1_key = uri_escape_RFC3986(Encode::encode("UTF-8", $oauth_consumer_secret)); $HMAC_SHA1_key .= "&"; $HMAC_SHA1_key .= uri_escape_RFC3986(Encode::encode("UTF-8", $oauth_token_secret)); my $hmac = Digest::HMAC_SHA1->new($HMAC_SHA1_key); $hmac->add($signature_base_str); my $signature = $hmac->b64digest; $signature .= "="; my $ua = LWP::UserAgent->new; my $req = POST($api_url => [ oauth_nonce => $oauth_nonce, oauth_signature_method => $oauth_signature_method, oauth_timestamp => $oauth_timestamp, oauth_consumer_key => $oauth_consumer_key, oauth_token => $oauth_token, oauth_signature => $signature, oauth_version => $oauth_version, status => $status ]); my $res = $ua->request($req); print "Content-type: text\/html\n\n"; if ($res->is_success) { print "success: $res->decoded_content"; } else { print "error:", $res->status_line; } #---------------------------------------------------- sub uri_escape_RFC3986 { my($str) = @_; return uri_escape($str,"^A-Za-z0-9\-_.~"); }
1
2
3
4
5
6
7
8
9
10
POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth realm="Example",
oauth_consumer_key="9djdj82h48djs9d2",
oauth_token="kkk9d7dh3k39sjv7",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="137131201",
oauth_nonce="7d8f3e4a",
oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"
1 2 3 4 5 6 7 8 9 10 11 12 13 14
my $request = HTTP::Request->new(POST => $api_url); $request->header( authorization => 'OAuth realm="https://api.twitter.com/oauth/', oauth_nonce => $oauth_nonce, oauth_signature_method => $oauth_signature_method, oauth_timestamp => $oauth_timestamp, oauth_consumer_key => $oauth_consumer_key, oauth_token => $oauth_token, oauth_signature => $signature, oauth_version => $oauth_version status => $status ); $request->content_type("text/xml; charset=utf-8"); my $res = $ua->request($request);
Guest Lars[...]
In der OAuth Doku (http://tools.ietf.org/html/rfc5849) und bei Twitter (http://dev.twitter.com/pages/auth) steht, was man alles an Twitter schicken muss, z.B.:
Code: (dl )1
2
3
4
5
6
7
8
9
10POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth realm="Example",
oauth_consumer_key="9djdj82h48djs9d2",
oauth_token="kkk9d7dh3k39sjv7",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="137131201",
oauth_nonce="7d8f3e4a",
oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"
Und so habe ich das versucht:
Code (perl): (dl )1 2 3 4 5 6 7 8 9 10 11 12 13 14my $request = HTTP::Request->new(POST => $api_url); $request->header( authorization => 'OAuth realm="https://api.twitter.com/oauth/', oauth_nonce => $oauth_nonce, oauth_signature_method => $oauth_signature_method, oauth_timestamp => $oauth_timestamp, oauth_consumer_key => $oauth_consumer_key, oauth_token => $oauth_token, oauth_signature => $signature, oauth_version => $oauth_version status => $status ); $request->content_type("text/xml; charset=utf-8"); my $res = $ua->request($request);
Leider kommt als Antwort immer 401 not authorized...
[...]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
sub join_pairs { my ($separator, @items) = @_; local ($a, $b); my @result; push @result, "$a$separator$b" while (($a, $b, @items) = @items); @result; } my $request = HTTP::Request->new(POST => $api_url); $request->header( authorization => 'OAuth ' . join(',', join_pairs('=', realm => "https://api.twitter.com/oauth/", oauth_nonce => $oauth_nonce, oauth_signature_method => $oauth_signature_method, oauth_timestamp => $oauth_timestamp, oauth_consumer_key => $oauth_consumer_key, oauth_token => $oauth_token, oauth_signature => $signature, oauth_version => $oauth_version, status => $status )) ); $request->content_type("text/xml; charset=utf-8"); my $result = $agent->request($request);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#!/usr/bin/perl # # Net::Twitter - OAuth desktop app example # use warnings; use strict; use Net::Twitter; use File::Spec; use Storable; use Data::Dumper; # You can replace the consumer tokens with your own; # these tokens are for the Net::Twitter example app. my %consumer_tokens = ( consumer_key => 'v8t3JILkStylbgnxGLOQ', consumer_secret => '5r31rSMc0NPtBpHcK8MvnCLg2oAyFLx5eGOMkXM', ); # $datafile = oauth_desktop.dat my (undef, undef, $datafile) = File::Spec->splitpath($0); $datafile =~ s/\..*/.dat/; my $nt = Net::Twitter->new(traits => [qw/API::REST OAuth/], %consumer_tokens); my $access_tokens = eval { retrieve($datafile) } || []; if ( @$access_tokens ) { $nt->access_token($access_tokens->[0]); $nt->access_token_secret($access_tokens->[1]); } else { my $auth_url = $nt->get_authorization_url; print " Authorize this application at: $auth_url\nThen, enter the PIN# provided to contunie: "; my $pin = <STDIN>; # wait for input chomp $pin; # request_access_token stores the tokens in $nt AND returns them my @access_tokens = $nt->request_access_token(verifier => $pin); # save the access tokens store \@access_tokens, $datafile; } my $status = $nt->user_timeline({ count => 1 }); print Dumper $status;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
my %oauth=( oauth_nonce => $oauth_nonce, oauth_signature_method => $oauth_signature_method, oauth_timestamp => $oauth_timestamp, oauth_consumer_key => $oauth_consumer_key, oauth_token => $oauth_token, oauth_signature => $signature, oauth_version => $oauth_version, ); my $oauth_str=join(", ", map{$_=qq($_="$oauth{$_}")} sort keys(%oauth)); my $request = HTTP::Request->new(POST => $api_url); $request->header( Authorization => 'OAuth realm="https://api.twitter.com/oauth/ ,'.$oauth_str, status => $status, ); $request->content_type("application/x-www-form-urlencoded"); my $res = $ua->request($request);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
my %oauth=( oauth_nonce => $oauth_nonce, oauth_signature_method => $oauth_signature_method, oauth_timestamp => $oauth_timestamp, oauth_consumer_key => $oauth_consumer_key, oauth_token => $oauth_token, oauth_signature => $signature, oauth_version => $oauth_version, ); my $oauth_str=join(", ", map{$_=qq($_="$oauth{$_}")} sort keys(%oauth)); my $request = HTTP::Request->new(POST => $api_url); $request->header( Authorization => 'OAuth realm="https://api.twitter.com/oauth/ ,'.$oauth_str); $request->content_type("application/x-www-form-urlencoded"); $request->content(status => $status); my $res = $ua->request($request);
1
2
3
4
5
6
7
8
9
10
$VAR1 = bless( {
'_content' => 'status',
'_uri' => bless( do{\(my $o = 'http://api.twitter.com/1/statuses/update.json')}, 'URI::http' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/5.814',
'content-type' => 'application/x-www-form-urlencoded',
'authorization' => 'OAuth realm="https://api.twitter.com/oauth/ ,oauth_consumer_key="0E1xSiyE03RTEms3WfQ", oauth_nonce="10000000022", oauth_signature="VD0q9A8qqk9l4QlDu8GrUbCTdAg=", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1272325550", oauth_token="88893619-JS87TMQDkUjM7Ufq119njayEJBuihVQmMEuTu3Ugi", oauth_version="1.0"'
}, 'HTTP::Headers' ),
'_method' => 'POST'
}, 'HTTP::Request' );
$request->content(qq[status=$status]);