我可以使用scribe获取tweet与特定的标签使用流api?我在java工作,我想使用抄写在一个应用引擎项目。这可能吗?我尝试下面的代码:
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>
正如我从你的代码中看到的,你正试图用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());