2011-06-01 4 views
2

J'ai été bloqué sur cela pendant un certain temps. Est-ce que quelqu'un sait comment authentifier les demandes de l'API Twitter Streaming en utilisant OAuth? En ce moment je m'authentifie via l'authentification de base, et je voudrais passer complètement à OAuth. En outre, j'utilise Ruby on Rails si cela peut vous aider.API de streaming Twitter avec OAuth?

Merci

Répondre

3

Connexion à l'API Twitter en streaming via OAuth est fait la même que la connexion via l'API REST. En supposant que vous ayez déjà négocié un jeton d'accès, vous signez et lancez la demande en utilisant le même algorithme de signature que pour une requête REST. Avec l'API Streaming, il est préférable d'utiliser l'OAuth en-tête plutôt que la chaîne de requête.

Voici un exemple d'une fin demande sur la base OAuth signé pour l'échantillon Point:

GET http://stream.twitter.com/1/statuses/sample.json

Signature exemple base chaîne:

GET & http% 3A% 2F% 2Fstream .twitter.com% 2Fstatuses% 2Fsample.json & oauth_consumer_key% 3Dri8JxYK2ddwSV5xIUfNNvQ% 26oauth_nonce% 3DUJb0f3nHhFQkpkWkJzxnFT65xX1TZeuGjww6Q2XWs4% 26oauth_signature_method% 3DHMAC-SHA1% 26oauth_timestamp% 3D1306947138% 26oauth_token% 3D819797-torCkTs0XK7H2Y2i1ee5iofqkMC4p7aayeEXRTmlw% 26oauth_version% 3D1.0

autorisation tête après la signature:

Autorisation: OAuth oauth_consumer_key = "ri8JxYK2ddwSV5xIUfNNvQ", oauth_nonce = "UJb0f3nHhFQkpkWkJzxnFT65xX1TZeuGjww6Q2XWs4", oauth_signature = "bN14zlBIdCZCSl9% 2B8UV8dB2VWjI% 3D", oauth_signature_method = "HMAC-SHA1", oauth_timestamp = "1306947138", oauth_token = "819797-torCkTs0XK7H2Y2i1ee5iofqkMC4p7aayeEXRTmlw", oauth_version = "1.0"

Matt Harris a quelques exemples de code en PHP montrant la connexion à l'API de transmission en continu via OAuth: https://github.com/themattharris/tmhOAuth/blob/master/examples/streaming.php

+0

Salut, j'ai regardé votre réponse et maintenant je comprends comment construire la chaîne de base et l'en-tête d'autorisation. Cependant, maintenant je suis coincé sur la façon de réellement poster les données correctement sur Twitter flux dans ruby. En ce moment, j'utilise yajl pour créer un httpstream à poster sur Twitter en utilisant uniquement l'authentification de base. –

1

Une fois que vous enregistrez votre application sur http://dev.twitter.com voici comment cela se fait dans Perl:

#!/usr/bin/perl 

use strict; 
use AnyEvent::Twitter::Stream; 

if ($ENV{FIREHOSE_SERVER}) { 
    $AnyEvent::Twitter::Stream::STREAMING_SERVER = $ENV{FIREHOSE_SERVER}; 
} 

my $done = AE::cv; 

binmode STDOUT, ":utf8"; 

my $streamer = AnyEvent::Twitter::Stream->new(
    consumer_key => 'KEY', 
    consumer_secret => 'SECRET', 
    token => 'TOKEN', 
    token_secret => 'TOKEN SECRET', 
    method => "filter", 
    track => "KEYWORDS TO TRACK", 
    on_tweet => sub { 
     # CUSTOM CODE HERE 
    }, 
    on_error => sub { 
     my $error = shift; 
     warn "ERROR: $error"; 
     $done->send; 
    }, 
    on_eof => sub { 
     $done->send; 
    }, 
); 

$done->recv;