Thread Twittern mit Perl und Oauth (10 answers)
Opened by Lars at 2010-09-02 23:50

murphy
 2010-09-03 17:13
#141159 #141159
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
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
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"


Und so habe ich das versucht:

Code (perl): (dl )
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);


Leider kommt als Antwort immer 401 not authorized...
[...]


In einem HTTP-Kopf werden Zeilen, die mit einem Leerzeichen beginnen, an den Wert für das vorangehende Feld angehängt. Nach dem Beispiel aus der Dokumentation zu schließen muss das also eher so aussehen:
Code (perl): (dl )
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);


(edit: vergessene Klammern in Zeile 4 ergänzt)
Last edited: 2010-09-03 17:35:38 +0200 (CEST)
When C++ is your hammer, every problem looks like your thumb.

View full thread Twittern mit Perl und Oauth