gpt4 book ai didi

java - HttpClientErrorException : 401 Unauthorized basic authentication

转载 作者:行者123 更新时间:2023-12-01 17:56:34 24 4
gpt4 key购买 nike

我正在尝试通过基本身份验证获取网址。我设置用户/密码如下。同样的凭证也在 postman 中工作。

String RELATIVE_IDENTITY_URL  = "http://my_url/api/core/v3/people/email/abc@example.com";
RestTemplate restTemplate;
Credentials credentials;

//1. Set credentials
credentials = new UsernamePasswordCredentials("admin", "admin");

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( AuthScope.ANY, credentials);

//2. Bind credentialsProvider to httpClient
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
CloseableHttpClient httpClient = httpClientBuilder.build();

HttpComponentsClientHttpRequestFactory factory = new
HttpComponentsClientHttpRequestFactory(httpClient);

//3. create restTemplate
restTemplate = new RestTemplate();
restTemplate.setRequestFactory(factory);

//4. restTemplate execute
String url = RELATIVE_IDENTITY_URL;

String xml = restTemplate.getForObject(url,String.class);
System.out.println("Done");

我认为凭据设置不正确。这里有什么问题吗?错误:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
at com.src.AuthRestService.main(AuthRestService.java:85)

最佳答案

您缺少 auth header 并在其余模板执行中设置凭据。

关于java - HttpClientErrorException : 401 Unauthorized basic authentication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285338/

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