使用授权标头发送时,Spring RestTemplate显示401未经授权的错误

jvidinwx  于 2022-09-18  发布在  Spring
关注(0)|答案(0)|浏览(487)

我试图使用以下代码访问我的Spring rest应用程序中的一个Microsoft flow post URL,但它给出了401错误。我的代码:

@RequestMapping(value = "/hookslistner", method = RequestMethod.POST)
    public ResponseEntity<Void> recieveWebhook(@RequestBody InventorySystemModel inventory,
            @RequestHeader("event") String event,
            @RequestHeader("Authorization") String authorization) {
// authorization = "Basic <Base64 encoded value of username:pwd>"

        RestTemplate restTemplate = new RestTemplate();

        org.springframework.http.HttpHeaders httpHeaders = new org.springframework.http.HttpHeaders();

        String url = "https://prod-01.centralindia.logic.azure.com/workflows/835348<hiding rest of part>";        
        String headerName = "Authorization";
        httpHeaders.add(headerName, authorization);
        httpHeaders.add("Content-Type", "application/json");
        HttpEntity<String> requestEntity = new HttpEntity<>("Headers", httpHeaders);
        System.out.println(">>>>>>>" + restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody());
    }

错误:

SEVERE: Servlet.service() for servlet [webhooks] in context with path [/inventoryhooks] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 Unauthorized] with root cause
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
.
.
.

是不是因为我的目标url是https,而我的本地主机是http?

有谁能告诉我哪里出了问题吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题