英文:
Get bearer token and consume token from another API
问题
我已经创建了一个API来获取令牌,并在获取到令牌后将其用于同一个API中,以下是我创建的控制器:
@GetMapping("/getNik")
public String pushToken() {
// 生成令牌
String uri = "http://some-url-to-get-token";
String notEncoded = "*****:******";
String encodedAuth = Base64.getEncoder().encodeToString(notEncoded.getBytes());
System.out.println("encodeUserPass " + encodedAuth);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("client_id", "client_id");
headers.set("Authorization", "Basic " + encodedAuth);
HttpEntity<String> result = new HttpEntity<String>(headers);
String token = restTemplate.postForObject(uri, result, String.class);
Object obj = JSONValue.parse(token);
JSONObject jsonObject = (JSONObject) obj;
String access_token = (String) jsonObject.get("access_token");
// 获取令牌
System.out.println("baru di get " + access_token);
// 访问获取 nik 的 API
String urix = "http:/some-url-that-need-otp-to-access";
RestTemplate restTemplatex = new RestTemplate();
HttpHeaders headersx = new HttpHeaders();
headersx.setContentType(MediaType.APPLICATION_JSON);
headersx.set("Authorization", "Bearer " + access_token);
System.out.println("initokennya " + access_token);
HttpEntity<String> resultx = new HttpEntity<String>(headersx);
System.out.println("sama?" + resultx);
// 失败
String get = restTemplatex.getForObject(urix, String.class, resultx);
return get;
}
问题是当我获取到令牌后,尝试访问API时,但无法使用它,出现401未经授权的错误,是否有我遗漏的东西?
英文:
I have created an API to get bearer token and use the token on the same API as I got the token, here is the controller I have created:
@GetMapping("/getNik")
public String pushToken() {
//generate token
String uri = "http://some-url-to-get-token";
String notEncoded ="*****:******";
String encodedAuth = Base64.getEncoder().encodeToString(notEncoded.getBytes());
System.out.println("encodeUserPass "+encodedAuth);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("client_id", "client_id");
headers.set("Authorization", "Basic "+encodedAuth);
HttpEntity<String> result = new HttpEntity<String>(headers);
String token = restTemplate.postForObject(uri, result, String.class);
Object obj=JSONValue.parse(token);
JSONObject jsonObject = (JSONObject) obj;
String access_token = (String) jsonObject.get("access_token");
//dapat token
System.out.println("baru di get "+access_token);
//hit get nik
String urix = "http:/some-url-that-need-otp-to-access";
RestTemplate restTemplatex = new RestTemplate();
HttpHeaders headersx = new HttpHeaders();
headersx.setContentType(MediaType.APPLICATION_JSON);
headersx.set("Authorization", "Bearer "+access_token);
System.out.println("initokennya "+access_token);
HttpEntity<String> resultx = new HttpEntity<String>(headersx);
System.out.println("sama?"+resultx);
//fail
String get = restTemplatex.getForObject(urix ,String.class, resultx);
return get;
}
The problem is when I got the token, I try to hit the API, but I can't use it, error 401 Unauthorized is there something I missed?
答案1
得分: 2
尝试使用这个代码:
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
英文:
try with this
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论