gpt4 book ai didi

java - Twitter oauth 无效的 oauth_verifier 参数

转载 作者:搜寻专家 更新时间:2023-10-31 20:19:04 24 4
gpt4 key购买 nike

我正在通过 Java 实现 oauth,顺序如下:

1) 发送 POST https://api.twitter.com/oauth/request_token(带回调)Twitter 响应包含 oauth_token、oauth_token_secret 和 oauth_callback_confirmed=true

2) 重定向到 https://api.twitter.com/oauth/authenticate?oauth_token={oauth_token 来自 twitter 的先前响应}

3) 出现 Twitter 登录表单,我单击“登录”按钮。

4) Twitter 重定向到 {callback_url}?oauth_token={this token equals token from oauth/request_token response}&oauth_verifier={verifier}

5) 发布 https://api.twitter.com/oauth/access_tokenOauth header 包含 oauth_token,消息正文包含 oauth_verifier={returned verifier}

6) Twitter 响应=处理您的 OAuth 请求时出错:无效的 oauth_verifier 参数

oauth_verifier 有什么问题?

计算签名方法:

private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
SecretKey secretKey = null;

byte[] keyBytes = keyString.getBytes();
secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);

byte[] text = baseString.getBytes();

return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}

第一个请求的代码:

String oauth_signature_method = "HMAC-SHA1";

// generate any fairly random alphanumeric string as the "nonce".
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string;

// get the timestamp
Calendar tempcal = Calendar.getInstance();
long ts = tempcal.getTimeInMillis();
String oauth_timestamp = (new Long(ts / 1000)).toString();
String parameter_string = "oauth_callback=" + OauthConstants.TWITTER_OAUTH_CALLBACK
+ "&oauth_consumer_key=" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY
+ "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method="
+ oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp + "&oauth_version=1.0";
String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);
String oauth_signature = "";

try {
oauth_signature = computeSignature(signature_base_string, OauthConstants.TWITTER_OAUTH_CONSUMER_SECRET + "&");
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
...}

String twitter_endpoint = "https://api.twitter.com/oauth/request_token";
String authorization_header_string = "OAuth oauth_callback=\"" + OauthConstants.TWITTER_OAUTH_CALLBACK
+ "\",oauth_consumer_key=\"" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY
+ "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp
+ "\",oauth_nonce=\"" + oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\""
+ encode(oauth_signature) + "\"";

// Apache httpcore 4.4.1
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new RequestContent())
.add(new RequestTargetHost())
.add(new RequestConnControl())
.add(new RequestUserAgent("ApacheHttp/1.1"))
.add(new RequestExpectContinue(true)).build();

HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpCoreContext context = HttpCoreContext.create();
HttpHost host = new HttpHost(twitter_endpoint_host, 443);
DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);

context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);

try {
// initialize the HTTPS connection
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, null, null);
SSLSocketFactory ssf = sslcontext.getSocketFactory();
Socket socket = ssf.createSocket();
socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), 0);
conn.bind(socket);

BasicHttpEntityEnclosingRequest request2 = new BasicHttpEntityEnclosingRequest("POST", twitter_endpoint_path, HttpVersion.HTTP_1_1);
request2.setEntity(new StringEntity("", "UTF-8"));
request2.addHeader("Authorization", authorization_header_string);
httpexecutor.preProcess(request2, httpproc, context);
HttpResponse response2 = httpexecutor.execute(request2, conn, context);
httpexecutor.postProcess(response2, httpproc, context);
} catch(Exception e) {} ...

第二个请求的代码(重定向到 https oauth/authenticate)

public JSONObject getTwitterAuthorizationCodeFromRequestToken(String oauth_token) {
...
String twitter_endpoint = "https://api.twitter.com/oauth/authenticate";

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(twitter_endpoint + "?oauth_token=" + encode(oauth_token));
} catch (IOException ex) {...}
...
}

第三个请求的代码(POST oauth/access_token)

    public JSONObject getTwitterAccessTokenFromAuthorizationCode(String verifier_or_pin, String oauth_token) {
...
String oauth_signature_method = "HMAC-SHA1";

// generate any fairly random alphanumeric string as the "nonce". Nonce = Number used ONCE.
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string;

Calendar tempcal = Calendar.getInstance();
long ts = tempcal.getTimeInMillis();
String oauth_timestamp = (new Long(ts / 1000)).toString();

// the parameter string must be in alphabetical order
String parameter_string = "oauth_consumer_key=" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY
+ "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method
+ "&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + encode(oauth_token) + "&oauth_version=1.0";

String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);

String oauth_signature = "";
try {
oauth_signature = computeSignature(signature_base_string, OauthConstants.TWITTER_OAUTH_CONSUMER_SECRET + "&");
} catch (GeneralSecurityException | UnsupportedEncodingException e) {
...
}

String authorization_header_string = "OAuth oauth_consumer_key=\"" + OauthConstants.TWITTER_OAUTH_CONSUMER_KEY
+ "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp
+ "\",oauth_nonce=\"" + oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\""
+ encode(oauth_signature) + "\",oauth_token=\"" + encode(oauth_token) + "\"";

HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new RequestContent())
.add(new RequestTargetHost())
.add(new RequestConnControl())
.add(new RequestUserAgent("ApacheHttp/1.1"))
.add(new RequestExpectContinue(true)).build();

HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpCoreContext context = HttpCoreContext.create();
HttpHost host = new HttpHost(twitter_endpoint_host, 443);
DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);

context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, host);

try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, null, null);
SSLSocketFactory ssf = sslcontext.getSocketFactory();
Socket socket = ssf.createSocket();
socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), 0);
conn.bind(socket);

BasicHttpEntityEnclosingRequest request2 = new BasicHttpEntityEnclosingRequest("POST", twitter_endpoint_path);
// Including oauth_verifier value to request body
request2.setEntity(new StringEntity("oauth_verifier=" + encode(verifier_or_pin), "UTF-8"));
request2.addHeader("Authorization", authorization_header_string);
httpexecutor.preProcess(request2, httpproc, context);
HttpResponse response2 = httpexecutor.execute(request2, conn, context);
...
}

最佳答案

我在使用 JavaScript 代码库时遇到过完全相同的情况。经过一天的努力,我找到了解决错误的解决方案。这只是添加一个值为“application/x-www-form-urlencoded”的“Content-Type” header 。

我的代码过去可以正常工作,但最近几个月就停止工作了。我的猜测是 Twitter 最近更改了 OAuth 处理的实现,这迫使我们明确添加内容类型。

关于java - Twitter oauth 无效的 oauth_verifier 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30550272/

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