问题
I am getting the following error while making the "user/info" endpoint on Tumblr API.
Error Response: "{"meta":{"status":401,"msg":"Unauthorized"},"response":[],"errors":[{"title":"Unauthorized","code":1016,"detail":"Unable to authorize"}]}
Debugged everything(Signature and OAuth Header) and can't find the exact problem. Any sample code for Tumblr API: "user/info" endpoint with the signature and request header process in JAVA.
My code:
String query = "https://api.tumblr.com/v2/user/info";
String consumerKey = "XYZ.....SDFD";
String consumerSecret = "XYZ....SDFSDF";
String oauth_token = "XYS....SFDSF";
String oauth_token_secret= "XYS....SFDSF";
int millis = (int) System.currentTimeMillis() * -1;
int time = (int) ((System.currentTimeMillis()) / 1000);
String signatureBaseString = "GET" + "&" + URLEncoder.encode(query, "UTF-8") + "&" + URLEncoder.encode("oauth_consumer_key=" + URLEncoder.encode(consumerKey, "UTF-8") + "&oauth_nonce=" + URLEncoder.encode(String.valueOf(millis), "UTF-8") + "&oauth_signature_method=" + URLEncoder.encode("HMAC-SHA1", "UTF-8") + "&oauth_timestamp=" + URLEncoder.encode(String.valueOf(time), "UTF-8") + "&oauth_token=" + URLEncoder.encode(oauth_token, "UTF-8") + "&oauth_version=" + URLEncoder.encode(OAuthConstants.OAUTHVERSION, "UTF-8"), "UTF-8");//No I18N
String signatureKey = URLEncoder.encode(consumerSecret, "UTF-8") + "&" + URLEncoder.encode(oauth_token_secret, "UTF-8");
SecretKeySpec secret = new SecretKeySpec(signatureKey.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secret);
byte[] digest = mac.doFinal(signatureBaseString.getBytes());
Base64Encoder enc = new Base64Encoder();
String signature = enc.encode(digest);
String requestHeader = OAuthConstants.OAUTH + " oauth_consumer_key=\"" + URLEncoder.encode(consumerKey, "UTF-8") + "\"" + ", oauth_nonce=\"" + URLEncoder.encode(String.valueOf(millis), "UTF-8") + "\"" + ", oauth_signature=\"" + URLEncoder.encode(signature, "UTF-8") + "\"" + ", oauth_signature_method=\"" + URLEncoder.encode("HMAC-SHA1", "UTF-8") + "\"" + ", oauth_timestamp=\"" + URLEncoder.encode(String.valueOf(time), "UTF-8") + "\"" + ", oauth_token=\"" + URLEncoder.encode(oauth_token, "UTF-8") + "\"" + ", oauth_version=\"" + URLEncoder.encode(OAuthConstants.OAUTHVERSION, "UTF-8") + "\"";//No I18N
HashMap<String, String> setrequestProperty = new HashMap<String, String>();
setrequestProperty.put("Authorization", requestHeader);//No I18N
URL url = new URL(query);
HttpsURLConnection httpsconnection = (HttpsURLConnection) url.openConnection();
httpsconnection.setConnectTimeout(30000);
httpsconnection.setReadTimeout(30000);
httpsconnection.setRequestMethod("GET");
httpsconnection.setDoInput(true);
httpsconnection.setDoOutput(false);
for (Map.Entry<String, String> entry : setrequestProperty.entrySet()) {
httpsconnection.setRequestProperty(entry.getKey(),entry.getValue());
}
int responseCode = httpsconnection.getResponseCode();
InputStream responseStream = null;
if (responseCode >= 400) {
responseStream = httpsconnection.getErrorStream();
} else if (responseCode != 0) {
responseStream = httpsconnection.getInputStream();
}
kindly someone helps me with it.
Thanks,
Madhubala.
来源:https://stackoverflow.com/questions/59302511/tumblr-api-user-info-endpoint-getting-401-unauthorized