401向Twitter API发送GET users/show时出错
我试图通过使用get users/show获取已授权我的应用程序的twitter用户的用户详细信息。为此,我使用的是原始Java,而不是Twitter API(因此缺少文档)。我能够成功地获取请求令牌和访问令牌,但在最后一步尝试获取用户信息时出现401错误。有人注意到我做错了什么吗401向Twitter API发送GET users/show时出错,twitter,twitter-oauth,Twitter,Twitter Oauth,我试图通过使用get users/show获取已授权我的应用程序的twitter用户的用户详细信息。为此,我使用的是原始Java,而不是Twitter API(因此缺少文档)。我能够成功地获取请求令牌和访问令牌,但在最后一步尝试获取用户信息时出现401错误。有人注意到我做错了什么吗 int t = (int) ((System.currentTimeMillis()) / 1000); String n = generateNonce(); String parameterStr
int t = (int) ((System.currentTimeMillis()) / 1000);
String n = generateNonce();
String parameterStr = "oauth_consumer_key=" + TWITTER_KEY +
"&oauth_nonce=" + n +
"&oauth_signature_method=" + oauthSignatureMethod +
"&oauth_timestamp=" + t +
"&oauth_token=" + USER_TOKEN +
"&oauth_version=1.0" +
"&screen_name=" + USER_SCREEN_NAME;
String oauthSignature = "";
String authorizationHeaderStr = "";
try {
String signatureBaseStr = "GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fusers%2Fshow.json&" + URLEncoder.encode(parameterStr, "UTF-8");
oauthSignature = URLEncoder.encode(computeSignature(signatureBaseStr, TWITTER_SECRET + "&" + USER_SECRET_TOKEN), "UTF-8");
authorizationHeaderStr = "OAuth oauth_consumer_key=\"" + TWITTER_KEY +
"\",oauth_nonce=\"" + n +
"\",oauth_signature=\"" + oauthSignature +
"\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + t +
"\",oauth_token=\"" + USER_TOKEN +
"\",oauth_version=\"1.0\"";
System.out.println(signatureBaseStr);
System.out.println(authorizationHeaderStr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
//Create post and receive oauth token
try {
String urlStr = "https://api.twitter.com/1.1/users/show.json?" + "screen_name=" + USER_SCREEN_NAME;
URL url = new URL(urlStr);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", authorizationHeaderStr);
//Send post
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.flush();
wr.close();
//Get post
int twitterResponseCode = conn.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer twitterResponse = new StringBuffer();
while((inputLine = in.readLine()) != null) {
twitterResponse.append(inputLine);
}
in.close();
System.out.println(twitterResponse);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
问题是outputstream:
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.flush();
wr.close();
我把它去掉了,一切正常。老实说,我不知道为什么它一开始就在那里