[英]How to use the twitter4j lib to get the tweets of screen name?
我已經看過很多使用這個庫的教程,但是我對它有一個清晰的認識。
首先,我如何驗證Twitter應用程序?,
有什么方法可以對訪問令牌進行硬編碼,從而使用戶不必執行任何操作即可通過輸入屏幕名稱直接搜索特定用戶的推文?
提及屏幕名稱后如何獲得推文?
我試着用twitter4j lib閱讀文檔,但是它對我有幫助。
我需要兩天后的幫助,Plz幫助...
有多種驗證方式:
首先,您將需要在此處創建一個應用程序。 然后,您將收到您的消費者密鑰和機密:
然后,您可以使用此代碼在啟動時請求授權。
public class MainActivity extends Activity {
// TwitterProperties
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public final static String consumerKey = "YOUR CONSUMER KEY";
public final static String consumerSecret = "YOUR CONSUMER SECRET";
private final String CALLBACKURL = "SCHEME://HOST";
private Twitter twitter;
AccessToken a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.enableDefaults();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
doAuth();
}
private void doAuth() {
try {
httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,
consumerSecret);
httpOauthprovider = new DefaultOAuthProvider(
"https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
"https://twitter.com/oauth/authorize");
String authUrl = httpOauthprovider.retrieveRequestToken(
httpOauthConsumer, CALLBACKURL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(authUrl)));
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
String verifier = uri
.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
// this will populate token and token_secret in consumer
try {
httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
verifier);
} catch (OAuthMessageSignerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OAuthCommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Important part where it actually sets the authorization so you can use it
a = new AccessToken(httpOauthConsumer.getToken(),
httpOauthConsumer.getTokenSecret());
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
twitter.setOAuthAccessToken(a);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
為了完成這項工作,您需要對清單進行一些調整。
<uses-permission android:name="android.permission.INTERNET" />
singleInstance
<activity
android:name="com.example.eredivisietwitter.MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.VIEW" >
</action>
<category android:name="android.intent.category.DEFAULT" >
</category>
<category android:name="android.intent.category.BROWSABLE" >
</category>
<data
android:host="HOST"
android:scheme="SCHEME" >
</data>
</intent-filter>
確保活動中具有相同的主機和方案:
private final String CALLBACKURL = "SCHEME://HOST";
既然您已經成功授權了您的應用,則可以使用Twitter
對象請求時間表等。
例:
private void getTweets(String user) {
try {
List<Status> statuses;
statuses = twitter.getUserTimeline(user);
System.out.println("Showing @" + user + "'s user timeline.");
for (Status status : statuses) {
System.out.println("@" + status.getUser().getScreenName()
+ " - " + status.getText());
}
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
}
}
瞧!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.