diff --git a/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java b/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java index 08f3fcc10d78d56b123610c26d824bab579516f9..2d60c2626a84c0ff681897bacdd7d6118e4b4b34 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java @@ -27,6 +27,8 @@ import static me.zhyd.oauth.utils.GlobalAuthUtil.urlEncode; */ public class AuthTwitterRequest extends AuthDefaultRequest { + private static final String PREAMBLE = "OAuth"; + public AuthTwitterRequest(AuthConfig config) { super(config, TWITTER); } @@ -47,10 +49,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest { Map oauthParams = buildOauthParams(); oauthParams.put("oauth_callback", config.getRedirectUri()); oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", baseUrl, config.getClientSecret(), null)); - oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\"")); - + String header = buildHeader(oauthParams); HttpResponse requestToken = HttpRequest.post(baseUrl) - .header("Authorization", "OAuth " + GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ")) + .header("Authorization", header) .execute(); checkResponse(requestToken); @@ -75,10 +76,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest { oauthParams.put("oauth_token", authCallback.getOauthToken()); oauthParams.put("oauth_verifier", authCallback.getOauthVerifier()); oauthParams.put("oauth_signature", generateTwitterSignature(oauthParams, "POST", source.accessToken(), config.getClientSecret(), authCallback.getOauthToken())); - oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\"")); - + String header = buildHeader(oauthParams); HttpResponse response = HttpRequest.post(source.accessToken()) - .header("Authorization", "OAuth " + GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ")) + .header("Authorization", header) .header("Content-Type", "application/x-www-form-urlencoded") .form("oauth_verifier", authCallback.getOauthVerifier()) .execute(); @@ -108,10 +108,9 @@ public class AuthTwitterRequest extends AuthDefaultRequest { Map params = new HashMap<>(oauthParams); params.putAll(queryParams); oauthParams.put("oauth_signature", generateTwitterSignature(params, "GET", source.userInfo(), config.getClientSecret(), authToken.getOauthTokenSecret())); - oauthParams.forEach((k, v) -> oauthParams.put(k, "\"" + urlEncode(v.toString()) + "\"")); - + String header = buildHeader(oauthParams); HttpResponse response = HttpRequest.get(userInfoUrl(authToken)) - .header("Authorization", "OAuth " + GlobalAuthUtil.parseMapToString(oauthParams, false).replaceAll("&", ", ")) + .header("Authorization", header) .execute(); checkResponse(response); JSONObject userInfo = JSONObject.parseObject(response.body()); @@ -148,6 +147,22 @@ public class AuthTwitterRequest extends AuthDefaultRequest { return params; } + private String buildHeader(Map oauthParams) { + final StringBuilder sb = new StringBuilder(PREAMBLE); + + for (Map.Entry param : oauthParams.entrySet()) { + if (sb.length() > PREAMBLE.length()) { + sb.append(", "); + } + sb.append(param.getKey()) + .append("=\"") + .append(urlEncode(param.getValue().toString())) + .append('"'); + } + + return sb.toString(); + } + private void checkResponse(HttpResponse response) { if (!response.isOk()) { throw new AuthException(response.body());