Android 应用程序的 Twitter 集成
我需要将 Twitter 与我的 Android 应用程序集成,这是我的代码(基于 this):
package com.anta40.oauthsetup;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import twitter4j.Status;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class OAuthSetup extends Activity {
Twitter twitter;
RequestToken requestToken;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String CONSUMER_KEY = "xxxxxxxxxx";
String CONSUMER_SECRET = "yyyyyyyyyyyyyyyyyyyy";
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
RequestToken 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();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
try {
String verifier = uri.getQueryParameter("oauth_verifier");
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
String token = accessToken.getToken();
String secret = accessToken.getTokenSecret();
displayTimeLine(token, secret);
}
catch (TwitterException e){
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
if (null != token && null != secret) {
List<Status> statuses = null;
try {
twitter.setOAuthAccessToken(token, secret);
statuses = twitter.getFriendsTimeline();
Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
.show();
} catch (Exception ex) {
Toast.makeText(this, "Error:" + ex.getMessage(),
Toast.LENGTH_LONG).show();
Log.d("Main.displayTimeline",""+ex.getMessage());
}
} else {
Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
}
}
}
现在,我如何使用 Dialog 来输入 Twitter 用户名和密码,而不是使用 WebView?密码?
I need to integrate Twitter with my Android app, and this is my code (based on this):
package com.anta40.oauthsetup;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import twitter4j.Status;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class OAuthSetup extends Activity {
Twitter twitter;
RequestToken requestToken;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String CONSUMER_KEY = "xxxxxxxxxx";
String CONSUMER_SECRET = "yyyyyyyyyyyyyyyyyyyy";
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
RequestToken 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();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
try {
String verifier = uri.getQueryParameter("oauth_verifier");
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
String token = accessToken.getToken();
String secret = accessToken.getTokenSecret();
displayTimeLine(token, secret);
}
catch (TwitterException e){
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
if (null != token && null != secret) {
List<Status> statuses = null;
try {
twitter.setOAuthAccessToken(token, secret);
statuses = twitter.getFriendsTimeline();
Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
.show();
} catch (Exception ex) {
Toast.makeText(this, "Error:" + ex.getMessage(),
Toast.LENGTH_LONG).show();
Log.d("Main.displayTimeline",""+ex.getMessage());
}
} else {
Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
}
}
}
Now, instead of using WebView, how can I use Dialog to input the Twitter username & password?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。 Twitter 现在只允许第三方应用程序使用誓言对其自身进行身份验证。
好吧,让我澄清一下,你可以放置一个对话框并让用户在其中输入用户名/密码,但 Twitter 不再为其提供任何 api。
您可以在此处找到有关该主题的更多信息:http://dev.twitter.com/pages/oauth_faq
You can't. Twitter will only let 3rd party apps use oath to authenticate with itself now.
Well, let me clarify that, you can put a dialog box and let the user enter there username/password, but twitter doesn't supply any api's for it anymore.
You can find more information on the topic here: http://dev.twitter.com/pages/oauth_faq
https://dev.twitter.com/docs/oauth/xauth - 看看这个功能,也许对你有帮助
https://dev.twitter.com/docs/oauth/xauth - look at this feature, maybe it will helps you