Académique Documents
Professionnel Documents
Culture Documents
7ZLWWHU-&RGH([DPSOHV
English|Japanese|Korean
Main
Blog
CodeExamples
postaTweet
GettingTimeline
Sending/ReceivingDirectMessages
SearchforTweets
AsynchronousAPI
Paginationcontrol
OAuthsupport
SigninwithTwitter
StreamingAPI
Configuration
JavaDoc
APISupportmatrix
Versions
Development
FAQ
PoweredByTwitter4J
)RUN
6WDU
)ROORZ#WMBQHZV
CodeExamples
Samplecodesarelocatedatsrc/twitter4j/examples/andyoucanruneach
classsusingbin/className.cmd|sh.
Toruntheexamplecodes,youneedtohaveOAuthcredentialsconfiguredin
twitter4j.properties.SeeTwitter4JConfigurationforthedetail.
1.
postaTweet
Youcanupdate"Whatareyoudoing?"viaTwitter.updateStatus()
method.
KWWSWZLWWHUMRUJHQFRGHH[DPSOHVKWPOVWUHDPLQJ
7ZLWWHU-&RGH([DPSOHV
Seealsotwitter4j.examples.tweets.UpdateStatus.javaforthedetail.
2.
GettingTimeline
Twitter.get****Timeline()returnsaListoflatesttweetsfromuser's
hometimeline.
Seealsotwitter4j.examples.timeline.GetHomeTimeline.javaforthedetail.
Doesn'tworkonJava1.4,orProcessing?ChecktheFAQ!
3.
Sending/ReceivingDirectMessages
Youcansendandreceivedirectmessagesvia
Twitter.sendDirectMessage()/Twitter.getDirectMessages().
Seealsotwitter4j.examples.directmessage.SendDirectMessage.javafor
thedetail.
Doesn'tworkonJava1.4,orProcessing?ChecktheFAQ!
4.
SearchforTweets
YoucansearchforTweetsusingQueryclassand
Twitter.search(twitter4j.Query)method.
Seetwitter4j.examples.search.SearchTweets.javaforthedetail.
KWWSWZLWWHUMRUJHQFRGHH[DPSOHVKWPOVWUHDPLQJ
7ZLWWHU-&RGH([DPSOHV
Doesn'tworkonJava1.4,orProcessing?ChecktheFAQ!
5.
AsynchronousAPI
ItispossibletocallthetimeconsumingTwitterAPIsasynchronously
usingtwitter4j.AsyncTwitterclassalongwithTwitterListener.
Actualmethodcallswillbedoneinaseparatethreadandyoucangetthe
responsesthroughTwitterListenerinterface.
Seealsotwitter4j.examples.async.AsyncUpdate.javaforthedetail.
6.
Paginationcontrol
SomeAPIsupportspagination.ThoseAPIsacceptfollowingparameters:
YoucanusePagingclasstospecifythoseparameters.
NotethatsomeofaboveparametersarenotacceptedbythoseAPIs.
PleaserefertheSupportAPIMatrixtoseewhichparametersareaccepted
bywhichmethods.
KWWSWZLWWHUMRUJHQFRGHH[DPSOHVKWPOVWUHDPLQJ
7ZLWWHU-&RGH([DPSOHV
7.
OAuthsupport
WithOAuthauthorizationscheme,anapplicationcanaccesstheuser
accountwithoutuserid/passwordcombinationgiven.Youneedtoregister
yourapplicationathttp://twitter.com/oauth_clients/newtoacquire
consumerkey,andconsumersecretinadvance.key/secretpaircanbe
setviaTwitter#setOAuthConsumer(),orfollowingsystemproperties:
Initially,youdon'thaveapermissiontoaccesstheuser'saccountand
needtoacquireaccesstokenbyredirectingtheusertoanauthorization
URLasfollows:
KWWSWZLWWHUMRUJHQFRGHH[DPSOHVKWPOVWUHDPLQJ
7ZLWWHU-&RGH([DPSOHV
AfteryouacquiredtheAccessTokenfortheuser,theRequestTokenisnot
requiredanymore.YoucanpersisttheAccessTokentoanykindof
persistentstoresuchasRDBMS,orFilesystembyserializingtheobject,
orbygetingthetokenandthesecretfromAccessToken#getToken()and
AccessToken#getTokenSecret().
Seealso:TwitterAPIWiki/OAuthFAQ
8.
SigninwithTwitter
ItispossibletoauthenticateusersusingTwitteraccountswithyourweb
application.Toachievethat,simplypassacallbackURLupon
RequestTokenretrieval,andthengettheAccessTokenwiththe
oauth_verifierparameterwhichwillbeaddedtothecallbackURLupon
callback.
Anexampleimplementationisavailableat
http://github.com/yusuke/signinwithtwitter.
9.
StreamingAPI
TwitterStreamclasshasseveralmethodspreparedforthestreamingAPI.
KWWSWZLWWHUMRUJHQFRGHH[DPSOHVKWPOVWUHDPLQJ
7ZLWWHU-&RGH([DPSOHV
AllyouneedistohaveaclassimplementingStatusListener.Twitter4Jwill
docreatingathread,consumingthestream.
Seealsotwitter4j.examples.stream.PrintSampleStream.javaforthe
detail.
Doesn'tworkonJava1.4,orProcessing?ChecktheFAQ!
KWWSWZLWWHUMRUJHQFRGHH[DPSOHVKWPOVWUHDPLQJ