将 Twitter 集成到我的 Android 应用程序中
我正在将 Twitter 集成到我的 Android 应用程序中。它让我在 Twitter 上成功登录,但我不知道如何从中获取推文和状态,以便我可以在我的应用程序中显示它。这是我的代码。
String CONSUMER_KEY = "XXXXXXXXXXXXXXX";
String CONSUMER_SECRET = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
String url = requestToken.getAuthorizationURL();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
catch (TwitterException e){
e.printStackTrace();
}
任何帮助将不胜感激。
i am integrating the twitter in to my android application. and it logins me successfully on twitter but i do not know how to get the tweets and statuses from that so i can show it in my application. here is my code.
String CONSUMER_KEY = "XXXXXXXXXXXXXXX";
String CONSUMER_SECRET = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
String url = requestToken.getAuthorizationURL();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
catch (TwitterException e){
e.printStackTrace();
}
any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦您正确填充了 twitter 变量(即您的代码没有抛出异常),您就可以将其用于 twitter.getHomeTImeline() 等。
话虽如此,您的代码看起来只执行 OAuth 过程的第一部分,您仍然需要具有设置用户接收的 PIN 码的代码,然后创建完全 OAuth 授权的连接。
例如,查看 LoginActivity 的Zwitscher(v0.65 标签)。
getPinButton() 方法基本上就是上面的方法。当用户回来时,他在 EditText 中输入 pin 并单击
[setPinButton()][2]
,它提供了 OAuth 内容的第二部分。然后,OAuth 密钥和令牌将存储在首选项中以供以后使用(您需要它们通过 TwitterFactory 创建经过身份验证的 Twitter 实例(请参见例如
TwitterHelper.getTwitter()
了解如何执行此操作)。As soon as you have the
twitter
variable correctly populated (i.e. your code throws no Exception), you can use it totwitter.getHomeTImeline()
etc.Having said that, your code looks like it only does the first part of the OAuth procedure and that you still need to have code that sets the pin the user receives and then creates a fully OAuth authorized connection.
Have e.g. a look at LoginActivity of Zwitscher (v0.65 tag).
The method
getPinButton()
is basically what you have above. When the user comes back, he enters the pin in an EditText and clicks on the[setPinButton()][2]
which provides the 2nd part of the OAuth stuff.OAuth keyes and tokens are then stored in preferences for later use (you need them to create authenticated Twitter instances via the TwitterFactory (see e.g.
TwitterHelper.getTwitter()
on how to do this).如果您使用 signpost,您就可以处理登录部分,而无需用户输入 PIN 码,一旦你有了令牌和验证器,你就可以继续使用 twitter4j,使用
TwitterFactory twitterfact=new TwitterFactory(); 创建对象 twitter;
twitter = twitterfact.getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken);
现在您可以使用
twitter.getFriendsTimeLine()
显示时间线。这就是我所做的,而且效果很好。我可以发推文、阅读推文、发送私人消息……并且登录部分不会失败。You can handle the login part, without the user introducing the pin if you use signpost, and once you have the token and the verifier, you can go on with twitter4j, creating the object twitter with
TwitterFactory twitterfact=new TwitterFactory();
twitter = twitterfact.getOAuthAuthorizedInstance(consumerKey, consumerSecret,accessToken);
Now you can show the timeline with
twitter.getFriendsTimeLine()
. It's what I do, and it works fine. I can tweet, read tweets, send private messages... and the login part doesn't fail.