Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically embed Twitter timelines (API V 1.1)

Tags:

twitter

api

Twitter's new embedded timelines don't seem to allow dynamic creation of embedded timelines.

Here is the section on their site: https://dev.twitter.com/docs/embedded-timelines

In older versions of the API, it was possible to switch the username in the widget dynamically, but the new API forces you to store the full widget on their servers and access via a widget id. Any way around this?

like image 806
zackliscio Avatar asked Apr 12 '13 19:04

zackliscio


3 Answers

Nope :( Unfortunately, Twitter matches up all of the Widgets (embedded timelines are widgets) with their ids. As such, it recognizes your timeline widget's id and displays your timeline, thus you cannot simply change the Twitter handle that it queries.

Here are your options:

  1. Create multiple widgets and show/hide them based on some event
  2. Use AJAX and query a server-side script to grab a user's timeline.

For maximum flexibility, the second option is likely to be your best route. I can help you with that if you are using PHP or .NET, so let me know if you are (and which one).

like image 86
Zachary Kniebel Avatar answered Nov 14 '22 01:11

Zachary Kniebel


This works excellent for Timeline with the new Twitter API 1.1

1) Download twitter4j-core-3.0.3.jar in http://twitter4j.org/en/ 2) Try use this code:

private static final String TWITTER_CONSUMER_KEY = "xxxxxxxxxxxxxxxxxx";
private static final String TWITTER_SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static final String TWITTER_ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxx";
private static final String TWITTER_ACCESS_TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxx";

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
    .setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
    .setOAuthConsumerSecret(TWITTER_SECRET_KEY)
    .setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
    .setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
    Query query = new Query("MrEdPanama");
    QueryResult result;
    do {
        result = twitter.search(query);
        List<Status> tweets = result.getTweets();
        for (Status tweet : tweets) {
            System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
        }
    } while ((query = result.nextQuery()) != null);
    System.exit(0);
} catch (TwitterException te) {
    te.printStackTrace();
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.exit(-1);
}
like image 2
Erick Martinez Avatar answered Nov 14 '22 01:11

Erick Martinez


I realize this question was asked months ago, but this can be achieved quite easily actually, by adding "data-" tags in your URL.

Check Twitter page for customization of the embedded timelines: https://dev.twitter.com/docs/embedded-timelines#customization

like image 1
Sascha Avatar answered Nov 14 '22 02:11

Sascha