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

Gast Lars
 2010-09-02 23:50
#141125 #141125
Hi,

ich bin kein wirklicher Perl-Experte, habe aber mit Perl ein Webseite geschrieben, die ziemlich populär ist und wichtige Ereignisse automatisch in einen (ihren eigenen) Twitter-Account schreibt.

Jetzt hat Twitter das Verfahren auf OAuth umgestellt, und es funktioniert leider nichts mehr und aus der Twitter-Doku werde ich nicht richtig schlau.

Kann mir hier jemand helfen?

Ich habe meine Anwendung registriert und will meine eigenen Access Tokens verwenden, weil ich ja nur ein Single-User-Szenario habe.

Mein Programm sie so aus, aber die Antwort von Twitter ist immer nur "401 Unauthorized".

Das Modul NET:Twitter kann ich leider nicht benutzen, weil mein Provider es nicht installiert hat...

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
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\-_.~");
}

Last edited: 2010-09-03 00:23:47 +0200 (CEST)

View full thread Twittern mit Perl und Oauth