1

我有一个 curl 命令,它工作正常,但是当我在 RestTemplate 中尝试它时,我遇到了错误。

卷曲:

curl -X POST "https://api.example.com/v1/tokens/accessToken" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"refresh_token\": \"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsIng1dCI6IndKZlQ2NHpqemQxWk4yS1duX3FYSmpvOHByMCIsImtpZCI6Im9yYWtleSJ9.eyJzdWIiOm51bGwsIm9yYWNsZS5vYXV0aC51c2VyX29yaWdpbl9pZF90eXBlIjoiTERBUF9VSUQiLCJvcmFjbGUub2F1dGgudXNlcl9vcmlnaW5faWQiOiJQUk9BQ1QyMDE5ISIsImlzcyI6Ind3dy5vcmFjbGUuZXhhbXBsZS5jb20iLCJvcmFjbGUub2F1dGgucnQudHRjIjoicmVzb3VyY2VfYWNjZXNzX3RrIiwib3JhY2xlLm9hdXRoLnN2Y19wX24iOiJPQXV0aFNlcnZpY2VQcm9maWxlIiwiaWF0IjoxNTk1MjM3NDk0LCJvcmFjbGUub2F1dGgudGtfY29udGV4dCI6InJlZnJlc2hfdG9rZW4iLCJleHAiOjE1OTU4NDIyOTQsInBybiI6bnVsbCwianRpIjoiN2I0YzExZDAtNjRkZi00YjhkLWJkZTYtNmM2ODdkZTY4MDI2Iiwib3JhY2xlLm9hdXRoLnNjb3BlIjoiQXN1cFVzZXJQcm9maWxlLm1lIiwib3JhY2xlLm9hdXRoLmNsaWVudF9vcmlnaW5faWQiOiJNeUFzdXBBUEkiLCJ1c2VyLnRlbmFudC5uYW1lIjoiRGVmYXVsdERvbWFpbiIsIm9yYWNsZS5vYXV0aC5pZF9kX2lkIjoiMTIzNDU2NzgtMTIzNC0xMjM0LTEyMzQtMTIzNDU2Nzg5MDEyIn0.UjJKgHkCypoyIsK7-24vUdX4jT2vkODW56tfwNuAnFqvaS712ZdDAQFiu5wjS9sCajAx0LDoJXQoq5ThVliY71FU9cvwS80SRpXzEIX_TcnnOfNXyZBzTyCqKDPjin0ajZ9Jvc_LNbMMD15i0tN3U6IQHmahm8yikGL7lBtvT30\"}"

弹簧休息模板

    restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

    Map<String, Object> map = new HashMap<>();
    map.put("body", "{\"refresh_token\": \"eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsIng1dCI6IndKZlQ2NHpqemQxWk4yS1duX3FYSmpvOHByMCIsImtpZCI6Im9yYWtleSJ9.eyJzdWIiOm51bGwsIm9yYWNsZS5vYXV0aC51c2VyX29yaWdpbl9pZF90eXBlIjoiTERBUF9VSUQiLCJvcmFjbGUub2F1dGgudXNlcl9vcmlnaW5faWQiOiJQUk9BQ1QyMDE5ISIsImlzcyI6Ind3dy5vcmFjbGUuZXhhbXBsZS5jb20iLCJvcmFjbGUub2F1dGgucnQudHRjIjoicmVzb3VyY2VfYWNjZXNzX3RrIiwib3JhY2xlLm9hdXRoLnN2Y19wX24iOiJPQXV0aFNlcnZpY2VQcm9maWxlIiwiaWF0IjoxNTk1MjM4Njc4LCJvcmFjbGUub2F1dGgudGtfY29udGV4dCI6InJlZnJlc2hfdG9rZW4iLCJleHAiOjE1OTU4NDM0NzgsInBybiI6bnVsbCwianRpIjoiMDZiYWE1MjgtMTZmOC00MzU1LWE3ZmYtNDc3ZjA4ZTNmMGU1Iiwib3JhY2xlLm9hdXRoLnNjb3BlIjoiQXN1cFVzZXJQcm9maWxlLm1lIiwib3JhY2xlLm9hdXRoLmNsaWVudF9vcmlnaW5faWQiOiJNeUFzdXBBUEkiLCJ1c2VyLnRlbmFudC5uYW1lIjoiRGVmYXVsdERvbWFpbiIsIm9yYWNsZS5vYXV0aC5pZF9kX2lkIjoiMTIzNDU2NzgtMTIzNC0xMjM0LTEyMzQtMTIzNDU2Nzg5MDEyIn0.UCkScWFn8d3pupwAxilT8T-m6Rf8fgZ8ge9OZvWZbZ-4eCVCPyHeI8A1a2QV0_iZhjwHaITNTzCZ4Eydfkm-mZuCa-PLY63HJhWLzTGlbaYsWRVtHaLF58F6spZ106T6T3ZDvQVIsiQ9845ruG_1ib0vKV_57X_CcVB4XZktYvA\"}");

    HttpEntity<Map<String, Object>> entity = new HttpEntity<>(map, headers);

    String urlString = "https://api.example.com/v1/tokens/accessToken";
    String response = restTemplate.postForObject(urlString, entity, String.class);

例外:

Caused by: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [{"error":"Request Failed"}]

那么如何正确地将上面的 curl 转换为 RestTemplate 以避免服务器和没有正文错误。卷曲命令工作正常。

4

3 回答 3

1

尝试这个

    restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = 
             new HttpEntity<>("{\"refresh_token\": \"eyJhb...\"}", headers);
于 2020-07-20T15:05:11.533 回答
1

正如您正确注意到的那样,您应该使用:

map.put("refresh_token", "eyJhbGci...". 

但是你有 401 只是因为你传递了另一个令牌(检查 diff cURL 与 java 代码版本)

于 2020-07-20T15:21:10.503 回答
0

如果令牌具有类型,例如 Bearer,请检查您的令牌,您必须这样写:

"Authorization": "Bearer rrdedzfdgf........."

并确保 Bearer 和 token 之间只有一个空格

于 2022-02-18T01:11:57.400 回答