Tumblr API : “user/info” endpoint - Getting 401 - Unauthorized

十年热恋 提交于 2019-12-25 19:35:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!