아직 학습 중이지만 아래 코드에서 인증되지 않은 Error 401이 다시 발생합니다. POST 요청/Auth 헤더에 문제가 있다는 가정하에 OAuth 헤더가 우편 배달부에서 작동하는 것으로 알고 있습니까? 어떤 아이디어?OAuth 401 Unauthorized
//set timestamp
Long timestamp = System.currentTimeMillis()/1000;
//set nonce ***** call from main system*************************************************************
String aString = randomAlphaNumeric(11);
// other stuff
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
String url = "aURL";
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// String auth = Base64.getEncoder().encodeToString(credentials.getBytes());
List<NameValuePair> oauthHeaders = new ArrayList<>(9);
oauthHeaders.add(new BasicNameValuePair("oauth_consumer_key", "aKey"));
oauthHeaders.add(new BasicNameValuePair("oauth_nonce", aString));
oauthHeaders.add(new BasicNameValuePair("oauth_timestamp", String.valueOf(timestamp)));
oauthHeaders.add(new BasicNameValuePair("oauth_signature_method", "HMAC-SHA1"));
oauthHeaders.add(new BasicNameValuePair("oauth_version", "1.0"));
//generate signature
//encode
String encodedURL = encode(oauthHeaders.toString());
System.out.println("encoded URL:" +encodedURL);
//form base string
String baseString = "POST&"+encode(url).toString()+encodedURL;
System.out.println("Base String: "+baseString);
//form signature
byte[] byteHMAC = null;
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec spec;
if (null == secretKey) {
String signingKey = encode(secretKey) + '&';
spec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
} else {
String signingKey = encode(secretKey) + '&' + encode(secretKey);
spec = new SecretKeySpec(signingKey.getBytes(), "HmacSHA1");
}
mac.init(spec);
byteHMAC = mac.doFinal(baseString.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
String signature = new BASE64Encoder().encode(byteHMAC);
System.out.println("oauth signature: "+signature);
//set signature to params
oauthHeaders.add(new BasicNameValuePair("oauth_signature", signature));
String test = "OAuth "+oauthHeaders.toString();
headers.set("Authorization", test);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("Name",name.toString());
map.add("Region",region.toString());
HttpEntity<MultiValueMap<String, String>> requestEntity= new HttpEntity<MultiValueMap<String, String>>(headers, map);
System.out.println(requestEntity);
ResponseEntity<String> response= restTemplate.exchange(url ,HttpMethod.POST, requestEntity, String.class);
System.out.println(response.toString());
HttpStatus status = response.getStatusCode();
status.toString();
if(status.equals("200")){
Notification.show("Employer" + name +" added successfully");
}
else{
Notification.show("Unsuccessful, error: "+status);
}
}
URL과 소비자 키/서명이 분명한 이유로 삭제되었습니다.
다음 시스템 밖으로 인쇄뿐만 아니라 도움이 될 수 있습니다 :
인코딩 PARAMS을 : % 5Boauth_consumer_key % 3aKey % 2C % 20oauth_nonce % 3DWZU8H1B5JA6 % 2C % 20oauth_timestamp % 3D1511621759 % 2C % 20oauth_signature_method % 3DHMAC-SHA1 % 2C %의 20oauth_version % 3D1.0 % 5D
자료 문자열 : POST & HTTPS % 3A % 2F % 2Fapi.test.payrun.io % 2FEmployer % 5Boauth_consumer_key % 3aKey % 2C % 20oauth_nonce % 3DWZU8H1B5JA6 % 2C % 20oauth_timestamp % 3D1511621759 % 2C % 20oauth_signature_method % 3DHMAC-SHA1 % 2C % 20oauth_version % 3D1.0 % 5D
oauth 서명 : DlRJGSzgRIItzz + LzMbgnIfbOqU =
헤더 이름으로'Authorization' (S가 아닌 Z를 적어 둡니다)으로 시도해 보셨습니까? – bouteillebleu
방금 코드를 변경하고 코드를 실행합니다. 여전히 401 권한이 없습니다. –