我已經看過很多關於使用這個庫的教程,但我很清楚它的意思。如何使用twitter4j lib獲取屏幕名稱的推文?
首先,我如何可以驗證Twitter的應用程序??,
有什麼辦法,我可以硬編碼的訪問令牌,以便用戶一點兒也不做任何事情,他可以直接搜索特定用戶的鳴叫通過輸入屏幕名稱?
如何在提到屏幕名稱後得到推文?
我試着用twitter4j lib中讀取文檔,但它不力幫助我....
我需要幫助的IM卡在這兩個日子裏,plz幫助...
我已經看過很多關於使用這個庫的教程,但我很清楚它的意思。如何使用twitter4j lib獲取屏幕名稱的推文?
首先,我如何可以驗證Twitter的應用程序??,
有什麼辦法,我可以硬編碼的訪問令牌,以便用戶一點兒也不做任何事情,他可以直接搜索特定用戶的鳴叫通過輸入屏幕名稱?
如何在提到屏幕名稱後得到推文?
我試着用twitter4j lib中讀取文檔,但它不力幫助我....
我需要幫助的IM卡在這兩個日子裏,plz幫助...
有多種方式驗證:
The standard 3-legged authorization:我會解釋這短暫在這個答案。
Pin-based authorization:對於無法訪問或嵌入Web瀏覽器的應用程序。
xAuth:作爲應用程序授權的功能,因此用戶不需要登錄但使用應用程序進行授權。
首先,您需要創建一個應用程序here。 然後,您將收到您的使用者密鑰和機密:
然後,您可以使用此代碼以獲得授權的啓動。
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;
}
}
爲了做到這一點,您需要對您的Manifest進行一些調整。
<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());
}
}
Voilá!
但這裏的問題是我剛剛得到的鳴叫,我想名稱配置文件圖片和時間以及... – Saad
檢查出[thejavadoc](http://twitter4j.org/javadoc/index.html)用twitter4j來查看是可能的。 'UserResources'中有一個'showUser(long userId)',它返回'User'。一個''用戶'然後有'getProfileImageURL()'等 – James
我可以通過任何方法得到完整的JSON,就像它可能在API 1.0? – Saad