使用路标的Scala和Java的Twitter REST API
我們可以直接從Scala(或Java)中使用幾個開源庫,但據(jù)我所知它們使用的是舊的v1 API,而不是v1.1 API。 舊的API有非常嚴格的數(shù)據(jù)速率限制,新API中的限制要輕一些。 此外,我對原始數(shù)據(jù)更感興趣,使用Scala(或Java)解析返回的JSON并不難。
在Twitter上注冊應(yīng)用程序
首先,最簡單的入門方法是為您的Twitter帳戶注冊一個新應(yīng)用程序。 轉(zhuǎn)到https://dev.twitter.com/apps/new并創(chuàng)建一個新應(yīng)用程序。 不用擔(dān)心網(wǎng)址,因為我們不會使用OAuth回調(diào)機制:
根據(jù)您要對API進行的操作,需要為此應(yīng)用程序授予其他權(quán)限。 默認值為“只讀”,如果要允許新應(yīng)用程序發(fā)布或訪問直接消息,則需要更新權(quán)限。 這是通過應(yīng)用程序的設(shè)置頁面完成的:
創(chuàng)建應(yīng)用程序并設(shè)置正確的權(quán)限后,即可生成訪問令牌。 這樣做可以避免經(jīng)歷完整的OAuth舞蹈。 為此,請轉(zhuǎn)到新的應(yīng)用程序詳細信息,然后在底部選擇“創(chuàng)建我的訪問令牌”選項。
現(xiàn)在,您將擁有一組令牌(請參閱應(yīng)用程序的詳細信息部分):
我們將使用這些令牌來驗證對Twitter的請求。
使用OAuth庫
OAuth協(xié)議是一個非常好的文檔協(xié)議,但是您自己實現(xiàn)它需要大量工作并且容易出錯。 幸運的是,有許多OAuth庫可以為您提供幫助。 我試過一對,最簡單(至少對我來說)的是路標 。 以下示例顯示了如何從Scala執(zhí)行此操作,但是您可以對Java遵循相同的方法。
首先是依賴關(guān)系。 我使用了sbt,從路標上使用了支持HTTP公共資源的客戶端。 在sbt中添加以下內(nèi)容:
.. libraryDependencies ++= Seq('oauth.signpost' % 'signpost-core' % '1.2','oauth.signpost' % 'signpost-commonshttp4' % '1.2', 'org.apache.httpcomponents' % 'httpclient' % '4.2',... )對于Maven,您可以使用相同的庫。 接下來,我們可以編寫一個簡單的測試以查看是否一切正常。 在Java中,它看起來像這樣:
import oauth.signpost.OAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient;public class Tw {static String AccessToken = 'access token for your app';static String AccessSecret = 'access secret for your app';static String ConsumerKey = 'consumer key for your app';static String ConsumerSecret = 'consumer secret for your app';/*** @param args*/public static void main(String[] args) throws Exception {OAuthConsumer consumer = new CommonsHttpOAuthConsumer(ConsumerKey,ConsumerSecret);consumer.setTokenWithSecret(AccessToken, AccessSecret);HttpGet request = new HttpGet('http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=josdirksen');consumer.sign(request);HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(request);int statusCode = response.getStatusLine().getStatusCode();System.out.println(statusCode + ':' + response.getStatusLine().getReasonPhrase());System.out.println(IOUtils.toString(response.getEntity().getContent()));} }在Scala中,外觀幾乎相同:
import org.apache.http.client.HttpClient import org.apache.http.impl.client.DefaultHttpClient import org.apache.http.client.methods.HttpGet import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer import org.apache.commons.io.IOUtilsobject TwitterPull {val AccessToken = 'access token for your app';val AccessSecret = 'access secret for your app';val ConsumerKey = 'consumer key for your app';val ConsumerSecret = 'consumer secret for your app';def main(args: Array[String]) {val consumer = new CommonsHttpOAuthConsumer(ConsumerKey,ConsumerSecret);consumer.setTokenWithSecret(AccessToken, AccessSecret);val request = new HttpGet('http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=josdirksen');consumer.sign(request);val client = new DefaultHttpClient();val response = client.execute(request);println(response.getStatusLine().getStatusCode());println(IOUtils.toString(response.getEntity().getContent()));} }運行此命令時,輸出將如下所示:
200 {"previous_cursor_str":"0","next_cursor":0,"ids": [48342167,21011010,824959303,97242821,16953163,218083367,20869799,5234221,13604142,80 4783128,271050984,405121284,26470609,50201837,1723451,374494377,120867838,14311946,25 3114713,39554511,7375412,42507395,112806109,92787154,218238023,110443797,76922155,198 798790,294104985,305625416,217698029,21803482,14927822,15453445,15715866,15657036,186 956616,36028164,70380613,326158542,573546312,14401332,521488579,9108612,576970378,293 236313,16398366,16220300,15234937,32000283,439444353,14300622,67204409,155850135,1419 8255,32264673,15852981,313248158,20123099,608942046,234930032,36896958,18466675,45496 942,330899833,18980755,88253383,461023805,31175627,11044952,142780445,63175189,107991 607,94830953,600993241,6195002,115391430,550080945,381418927,168603682,142388604,8258 462,218411138,30450578,77728346,2521381,182867524,494119147,29426983,572417260,943448 49,325413275,389354525,501438275,164346498,22730282,8293302,21085554,341645357,569788 53,180507788,10074002,22536424,14247654,581293627,15259428,483317230,462826270,477464 1,15366832,96850673,278486993,22273826,17716679,14566626,158473088,20461042,161242434 ,43756629,40163100,141165981,5325152,7620782,266749648,524476136,557713614,39602637,1 8843154,1623,565954426,39639621,166672305,18683074,233118689,44876099,235258223,21931 0062,10699922,12660502,218030046,91552210,19361980,206645598,35346200,58440021,470388 557,26495649,59066453,40292255,543375441,33242290,6015852,317150447,22935775,23230034 6,476045917,90913482,249088920,67658976,614873,522722520,186766721,285517705,71683175 ,131444964,166501605,477920664,38154550,18738205,8861832,15594932,18536741,7595202,46 5378842,11838952,14848133,431696576,14358671,414520167,222578501,67058139,28976735,95 601387,426582611,24874129,418762594,128157235,106030956,31352215,18733178,260196778,1 53179029,91842580,229494512,83414433,285579699,19957600,54295155,14929418,51516573,20 0076011,18758733,17776895,59397841,216802709,149834999,327507356,8200322,174345369,10 8636400,27504001,326877592,139919716,49949338,215035403,118421144,49410665,149550914, 18446431,25662335,261725134,267634174,57737391,146506056,126964949,71055234,20870640, 210196418,222806923,13290742,72247756,180410163,14784480,36684216,25611502,95614691,5 4629161,112967594,181656257,17994312,72918901,140082918,149087212,137272324,99534020, 121755576,93964779,35848342,43059008,34704029,87672717,113137792,17863333,90407665,90 591814,54297023,57924897,87551006,28300354,48990752,26188013],"previous_cursor":0, "next_cursor_str":"0"如果得到403,請檢查令牌是否匹配。
祝您編程愉快,別忘了分享!
參考: 使用我們的JCG合作伙伴 Jos Dirksen在Smart Java博客上的路標 , 從Scala和Java訪問Twitter REST API(v1.1) 。
翻譯自: https://www.javacodegeeks.com/2012/09/twitter-rest-api-from-scala-and-java.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的使用路标的Scala和Java的Twitter REST API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么练习踢毽子 练习踢毽子的方法
- 下一篇: 世界的本原是什么 世界的本原指的是什么