如何创建一个java应用程序来读取twitter提要? [英] How to create a java application to read twitter feeds?

查看:120
本文介绍了如何创建一个java应用程序来读取twitter提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小应用程序,它将访问twitter api以仅使用应用程序级别身份验证来读取提要。我在网上看了很多文档,感到非常困惑。据我所知,twitter api需要OAth来授权任何应用程序从/向twitter获取或写入数据。为了获得关联的密钥(消费者密钥),twitter的开发应用程序页面要求我们创建一个新的应用程序,我创建该应用程序以获取密钥。现在我有一些教程说明如何创建一个属性文件来保存这些键并开始运行java应用程序。

I want to create a small application that will access twitter api to read feeds using application level authentication only. I have read many docs over net and feeling extremely confused. I understand that twitter api needs OAth to authorize any application to get or write data from/into twitter. For getting the keys associated(consumer secret key) the dev apps page of twitter asks us to create a new app which I created to get the keys. Now I have some tutorials that say how to make a properties file to save those keys and start running the java application.

问题是甚至在遵循我无法做到的一切之后运行应用程序。任何人都可以逐步描述如何创建一个java应用程序来读取提要,设置所需的所有配置,创建和解释获取密钥所需的所有步骤?
我正在使用twitter 4j。

The problem is even after following everything i am not able to run the application. Can anyone describe in a step by step method about creating a java application to read feeds, setting up all the configurations needed, creating and explaining all the steps needed to get the keys ? I am using twitter 4j.

推荐答案

创建一个java客户端使用第三个来读取twitter消息非常简单像Twitter4j这样的派对图书馆你没有提到你想要阅读的推文是你自己的推文还是别人的推文,无论如何我跟着这个博客并完成了我的工作:)

Its so simple to create a java client to read twitter messages using some third party libraries like Twitter4j etc. You did not mention which tweets you wanted to read whether your own tweets or someone else's, anyway I followed this blog and got my work done :)


  1. https://apps.twitter.com/app/new 上创建Twitter应用程序并获取您的消费者密钥。 (如果你碰到某个地方,请按照上面提到的博客。)

  2. 使用Twitter4j API获取授权URL,点击它并获取PIN。 (需要的代码在下面提到你的参考)

  3. 输入引脚并获取访问权限(参见下面的代码)

  4. 现在我们都可以阅读或更新Twitter Feed。 (基于您在创建Twitter应用程序时设置的访问级别)

  1. Crate a Twitter application on https://apps.twitter.com/app/new and get your consumer key. (Follow the mentioned blog if you struck somewhere.)
  2. Using the Twitter4j API get the authorization URL, hit it and get the PIN. (The code required is mentioned below for your ref)
  3. Input the pin and get access tocken (refer below code)
  4. That's all now we can read or update twitter feeds. (Based on the access level you set while creating twitter application)

示例代码:

 Twitter twitter = new TwitterFactory().getInstance();
 twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);

RequestToken requestToken = twitter.getOAuthRequestToken();
 System.out.println("Authorization URL: \n"
  + requestToken.getAuthorizationURL());

AccessToken accessToken = null;

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

   try{
       System.out.print("Hit above Authorization URL and Input PIN here: ");
       String pin = br.readLine();

       accessToken = twitter.getOAuthAccessToken(requestToken, pin);

     } catch (TwitterException te) {

        System.out.println("Failed to get access token, caused by: "
           + te.getMessage());
     }

 System.out.println("Access Token: " + accessToken.getToken());
 System.out.println("Access Token Secret: "
  + accessToken.getTokenSecret());

 // updating twitter status
 twitter.updateStatus("hi.. im updating this using Namex Tweet for Demo");

System.out.println("\nReading Twitter Timeline:");

 // I'm reading your timeline
 ResponseList list = twitter.getHomeTimeline();
 for (Status each : list) {

     System.out.println("Sent by: @" + each.getUser().getScreenName()
      + " - " + each.getUser().getName() + "\n" + each.getText()
      + "\n");
 }

如果您遇到任何问题,请在此处查看并发表评论。

Check it and comment here if you face any issues.

这篇关于如何创建一个java应用程序来读取twitter提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆