获取承载令牌并从另一个API消耗令牌

huangapple go评论61阅读模式
英文:

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(&quot;/getNik&quot;)
public String pushToken() {
//generate token
String uri = &quot;http://some-url-to-get-token&quot;; 
String notEncoded =&quot;*****:******&quot;;
String encodedAuth = Base64.getEncoder().encodeToString(notEncoded.getBytes());
System.out.println(&quot;encodeUserPass &quot;+encodedAuth);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set(&quot;client_id&quot;, &quot;client_id&quot;);
headers.set(&quot;Authorization&quot;, &quot;Basic &quot;+encodedAuth);
HttpEntity&lt;String&gt; result = new HttpEntity&lt;String&gt;(headers);
String token = restTemplate.postForObject(uri, result, String.class);
Object obj=JSONValue.parse(token); 
JSONObject jsonObject = (JSONObject) obj; 
String access_token = (String) jsonObject.get(&quot;access_token&quot;); 
//dapat token
System.out.println(&quot;baru di get &quot;+access_token);
//hit get nik
String urix = &quot;http:/some-url-that-need-otp-to-access&quot;; 
RestTemplate restTemplatex = new RestTemplate();
HttpHeaders headersx = new HttpHeaders();
headersx.setContentType(MediaType.APPLICATION_JSON);
headersx.set(&quot;Authorization&quot;, &quot;Bearer &quot;+access_token);
System.out.println(&quot;initokennya &quot;+access_token);
HttpEntity&lt;String&gt; resultx = new HttpEntity&lt;String&gt;(headersx);
System.out.println(&quot;sama?&quot;+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);

huangapple
  • 本文由 发表于 2023年7月6日 13:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定