gpt4 book ai didi

java - 使用 Scribe 获取带有特定主题标签的推文

转载 作者:行者123 更新时间:2023-12-01 14:29:09 25 4
gpt4 key购买 nike

我可以使用 scribe 通过流 API 获取带有特定主题标签的推文吗?我在 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>

最佳答案

正如我从您的代码中看到的,您尝试使用 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());

关于java - 使用 Scribe 获取带有特定主题标签的推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16956265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com