Twitter Streaming API - how to go about it? [SOLVED]



  • Hi,

    I'm trying to create a twitter stream client:

    I'm trying to use the twitter streaming api https://dev.twitter.com/docs/streaming-apis

    And using this example url on a web browser like firefox > https://stream.twitter.com/1.1/statuses/filter.json?track=imdb I get a permanent connection to twitter and tweets start showing up.

    I would like to mimic this behaviour with Qt5/QML but so far I'm kind of stuck...

    When accessing from the Web Browser, I get a user authentication popup. Once I login, the tweets start dropping down the pipe.

    Altering the URL to https://<user>:<pass>@stream.twitter.com/1.1/statuses/filter.json?track=imdb bypasses the popup but the site still issues another confirmation popup.

    Translating all this to Qt, I started reading about OAuth and how Twitter handles authentication. This lead me to kQOauth, an Oauth library for Qt > https://github.com/kypeli/kQOAuth

    I managed to get it to work, and with it a twittercli example application is included, much to my benefit: the example app works and basically fetches an authentication token, after providing user and pass, which will later use to send tweets.

    And the Qt cli app tweets just fine, using the stored authentication token.

    My objective would be to access the streaming APIs in the app much like the browser is doing, and for starters, display the tweets in the standard output - later on, emit signals to QML and update a Text element...

    But so far all I have gotten is 401 denied - and a sense that maybe something else other than just OAuth might be needed to establish the sockets like connection.

    If anyone has any pointers on this, please let me know!

    EDIT:

    Ok, managed to do it: I found QTweetLib, a very complete Qt Twitter library.

    First I had to head over to http://dev.twitter.com, create an account, and then create an application.
    This way, I was able to generate consumer key, consumer secret, access token and access token secret.

    With QTweetLib, there's a branch available for Qt5 - built ok but not all examples worked out of the box - and it was due to the fact that QTweetLib uses XAuth, and XAuth on Twitter requires that an app be authorized (sending an email to twitter, waiting for validation, etc etc etc)

    How to get past all this -> modify a few parts of the lib + examples and get a public stream client, just for demo/code analysis:

    https://github.com/minimoog/QTweetLib/tree/Qt5

    userstream.cpp

    @
    m_oauthTwitter = new OAuthTwitter(new QNetworkAccessManager, this);
    m_oauthTwitter->setOAuthToken("access_token"); << the auth token I generated on the dev.twitter.com site
    m_oauthTwitter->setOAuthTokenSecret("access_token_secret"); << the corresponding secret
    connect(m_oauthTwitter, SIGNAL(authorizeXAuthFinished()), SLOT(onAuthorizeFinished()));
    connect(m_oauthTwitter, SIGNAL(authorizeXAuthError()), SLOT(onAuthorizeFinished())); << XAuth will always fail because the example isn't authorized, so changing the slot function here allows everything to move on
    @

    qtweetuserstream.cpp

    change the URL to a public stream with a filter on it

    @
    //#define TWITTER_USERSTREAM_URL "https://userstream.twitter.com/2/user.json"
    #define TWITTER_USERSTREAM_URL "https://stream.twitter.com/1.1/statuses/filter.json?track=twitter"
    @

    oauth.cpp

    setting the consumer key / consumer secret from dev.twitter.com

    @
    #ifndef CONSUMER_KEY
    #define CONSUMER_KEY "" << fill this in
    #endif //CONSUMER_KEY

    #ifndef CONSUMER_SECRET
    #define CONSUMER_SECRET "" << fill this in
    #endif //CONSUMER_SECRET
    @

    at the end of this, building the userstream example works, running the example I got a Window Dialog - user and pass are irrelevant, click Connect, XAuth failure messages may appear but after a bit the tweets on the public stream should show up.

    At this point there is a working example on how Qt handles the Twitter Streaming API, based on QTweetLib - and I now have the means to understand how the signal / slot mechanisms work in order to have the tweets popping up!

    The shared library approach is also simple, the lib is built as an independent project and the demo app has the modified .pro file to include both the library directory containing the .so files and the header files as well.

    Something that didn't make much sense to me though, the libs shouldn't have the consumer key/consumer secret built in, since I could then use the built libs with multiple applications - so I think it would make more sense to just define the consumer key/ consumer secret in the actual applications code rather than on the lib code. But it is functional as it is, marked solved!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.