gpt4 book ai didi

java - RestTemplate 测试时出现错误请求

转载 作者:行者123 更新时间:2023-12-02 01:38:57 24 4
gpt4 key购买 nike

我有一个问题,当我使用 Maven 测试我的测试应用程序时,它显示 400 Bad Request。我使用带有基本身份验证和 grant_type password 的 oauth2。如果我用Postman之类的程序进行身份验证。有用。但如果我添加带有 Http-Headers 的基本身份验证,我会收到错误的请求。

我尝试从 HttpEntity 中删除 HttpHeaders() 对象,然后收到 401 Unauthorized。我尝试将加密 header 手动放入 HttpHeaders() 对象中,但这也导致了 400 Bad Request。如果我使用 Postman 执行请求,请选择 POST 作为方法,选择 Basic Auth 作为身份验证,使用用户名、密码、grant_type 作为 Body(JSON) 它可以工作。

这是我的测试类(class) @SpringBootTest 公共(public)类 TestAppApplicationTests {

    private RestTemplate restTemplate = new RestTemplate();


@Test
public void testOauthToken(){

String plainCreds = "client:topsecret";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);

Object oauthRequest = new OauthRequest("Lukas", "test123", "password");

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic "+ base64Creds);
HttpEntity<Object> reqData = new HttpEntity<>(oauthRequest, headers);

ResponseEntity<Object> entity = restTemplate.exchange("http://localhost:8080/oauth/token", HttpMethod.POST,
reqData,
Object.class);

}


}

这是我的OauthRequest

@Data
public class OauthRequest {
private String username;
private String password;
private String grant_type;

public OauthRequest(String username, String password, String grant_type){
this.username = username;
this.password = password;
this.grant_type = grant_type;
}
}

我想从服务器获取access_tokenrefresh_token

编辑:我这里有一个不起作用的存储库,如果您想了解有关代码的更多信息:https://github.com/precodeeu/test-spring-oauth

编辑²:如果我在其中放入原始 json 字符串,它也不起作用。

    OauthRequest oauthRequest = new OauthRequest("Lukas", "test123", "password");
String json;

try {
json = mapper.writeValueAsString(oauthRequest);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.add("Content-Type", "application/json");
HttpEntity<Object> reqData = new HttpEntity<>(json, headers);

ResponseEntity<Object> entity = restTemplate.exchange("http://localhost:8080/oauth/token", HttpMethod.POST,
reqData,
Object.class);

} catch (JsonProcessingException e) {
e.printStackTrace();
}

编辑³我刚刚注意到,内容类型应该是 Application_form_urlencoded

解决方案:我已经更新了代码并构建了一个 Stringbuilder

@SpringBootTest公共(public)类 TestAppApplicationTests {

private RestTemplate restTemplate = new RestTemplate();


private String UrlEncodedBuilder(List<String[]> list){
String returnString = "";
for(String[] param : list){
returnString+=param[0]+"="+param[1]+"&";
}

return returnString;
}

@Test
public void testOauthToken() {

String plainCreds = "client:topsecret";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);

List<String[]> formData = new ArrayList();

formData.add(new String[]{"username", "Lukas"});
formData.add(new String[]{"password", "test123"});
formData.add(new String[]{"grant_type", "password"});

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<Object> reqData = new HttpEntity<>(UrlEncodedBuilder(formData), headers);

ResponseEntity<Object> entity = restTemplate.exchange("http://localhost:8080/oauth/token", HttpMethod.POST,
reqData,
Object.class);


}
}

最佳答案

首先,调查您的请求到底是什么样子,并将其与有效的请求进行比较。如果出现 400 代码,还要检查响应正文,正文可能包含一些提示。

关于java - RestTemplate 测试时出现错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54760767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com