使用scribe获取带有特定标签的tweet

hl0ma9xz  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(394)

我可以使用scribe使用streeming api获取带有特定标签的tweet吗?我在java工作,我想在appengine项目中使用scribe。有可能吗?我尝试以下代码:

OAuthService service = new ServiceBuilder()
                            .provider(TwitterApi.class)
                            .apiKey(MYCONSUMERKEY)
                            .apiSecret(MYCONSUMERSECRET)         
                            .debug()                       
                            .build();

    OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    request.addBodyParameter("track", "prova");
    Token requestToken = new Token(MYACCESSTOKEN,MYACCESSTOKENSECRET);

    service.signRequest(requestToken, request);
    Response response = request.send();
    System.out.println(response.getBody());
    }

答案是:

signing request: https://stream.twitter.com/1/statuses/filter.json
  setting token to: Token[MYACCESSTOKEN,MYACCESSTOKENSECRET]
  generating signature...
  base string is: GET&https%3A%2F%2Fstream.twitter.com%2F1%2Fstatuses%2Ffilter.json&oauth_consumer_key%3DMYCONSUMERKEY%26oauth_nonce%3Dxxx%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3Dxxx%26oauth_token%3Dxxx%26oauth_version%3D1.0%26track%3Dprova
  signature is: 5UU55rZqzjiaRwSNGz/jrcffPoM=
  appended additional OAuth parameters: { oauth_signature -> xxx , oauth_version -> 1.0 , oauth_nonce -> xxx , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MYCONSUMERKEY , oauth_token -> xxx , oauth_timestamp -> xxx }
  using Http Header signature

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1/statuses/filter.json'. Reason:
<pre>    Unauthorized</pre>
fjnneemd

fjnneemd1#

正如我从代码中看到的,您正试图使用requesttoken对get调用进行签名,您必须首先获取一个accesstoken,然后使用这个accesstoken对任何get/post调用进行签名。试着这样做:

OAuthService service = new ServiceBuilder()
                        .provider(TwitterApi.class)
                        .apiKey(MYCONSUMERKEY)
                        .apiSecret(MYCONSUMERSECRET)         
                        .debug()                       
                        .build();

//get a request token
Token requestToken = service.getRequestToken();

//go to this url
System.out.println(service.getAuthorizationUrl(requestToken));

//after autorizing your twitter account you will see a blank page, in the url you will see 
//a parameter called oauth_verifier=XXXXXXXXXXX;
//paste the xxxx after this code: 

Verifier verifier = new Verifier(in.nextLine());

//now request an accessToken to service: 
Token accessToken = service.getAccessToken(requestToken, verifier);

//once you have accessToken, you should use it to do GET/POST calls:
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
//GET CALL DO NOT ACCEPT PARAMETER
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println(response.getBody());

//POST CALL
OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL);
request.addBodyParameter("track", "prova");
service.signRequest(accessToken, request);
Response response = request.send();
System.out.println(response.getBody());

相关问题