我们可以直接从Scala(或Java)中使用几个开源库,但据我所知它们使用的是旧的v1 API,而不是v1.1 API。 旧的API有非常严格的数据速率限制,新API中的限制要轻一些。 此外,我对原始数据更感兴趣,使用Scala(或Java)解析返回的JSON并不难。 在Twitter上注册应用程序

首先,最简单的入门方法是为您的Twitter帐户注册一个新应用程序。 转到https://dev.twitter.com/apps/new并创建一个新应用程序。 不用担心网址,因为我们不会使用OAuth回调机制:

根据您要对API进行的操作,需要为此应用程序授予其他权限。 默认值为“只读”,如果要允许新应用程序发布或访问直接消息,则需要更新权限。 这是通过应用程序的设置页面完成的:

创建应用程序并设置正确的权限后,即可生成访问令牌。 这样做可以避免经历完整的OAuth舞蹈。 为此,请转到新的应用程序详细信息,然后在底部选择“创建我的访问令牌”选项。

现在,您将拥有一组令牌(请参阅应用程序的详细信息部分):

我们将使用这些令牌来验证对Twitter的请求。 使用OAuth库

OAuth协议是一个非常好的文档协议,但是您自己实现它需要大量工作并且容易出错。 幸运的是,有许多OAuth库可以为您提供帮助。 我试过一对,最简单(至少对我来说)的是路标 。 以下示例显示了如何从Scala执行此操作,但是您可以对Java遵循相同的方法。

首先是依赖关系。 我使用了sbt,从路标上使用了支持HTTP公共资源的客户端。 在sbt中添加以下内容:

..
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.IOUtils
 
 
object 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