如何在org.openapitools.client.apiclient中使用用户和密码添加基本实施
我使用此openApigenerator
https://openapi-generator.tech/docs.tech/docs /generators/java 生成Java Client-App,因此创建一个Apiclient类,因此使用此类i创建请求;在示例代码中,它显示了以下内容:
import org.openapitools.client.api.WorkspaceApi;
import org.openapitools.client.model.WorkspaceReadList;
public class App {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://serveprodhost/api");
System.out.println(defaultClient.getAuthentications());
WorkspaceApi apiInstance = new WorkspaceApi(defaultClient);
try {
WorkspaceReadList result = apiInstance.listWorkspaces();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling WorkspaceApi#listWorkspaces");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
使用此代码,我需要使用基本实施(密码和用户名)连接到生产服务器,因此如何将这些凭据添加到此代码中。
我已经尝试过,但是它不起作用,返回例外:
///// java code
System.out.println(defaultClient.getAuthentications());
HttpBasicAuth basicAuthentication = (HttpBasicAuth) defaultClient.getAuthentication("basicAuth");
basicAuthentication.setUsername("admin");
basicAuthentication.setPassword("pass");
///异常MSJ
{basicAuth=org.openapitools.client.auth.HttpBasicAuth@45c7e403, bearerAuth=org.openapitools.client.auth.HttpBearerAuth@2925bf5b}
Exception when calling WorkspaceApi#listWorkspaces
Status code: 401
Reason: <html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>
Response headers: {content-length=[172], content-type=[text/html], date=[Wed, 27 Apr 2022 19:47:14 GMT], strict-transport-security=[max-age=15724800; includeSubDomains], www-authenticate=[Basic realm="Authentication Required"]}
org.openapitools.client.ApiException:
at org.openapitools.client.ApiClient.handleResponse(ApiClient.java:1081)
at org.openapitools.client.ApiClient.execute(ApiClient.java:994)
at org.openapitools.client.api.WorkspaceApi.listWorkspacesWithHttpInfo(WorkspaceApi.java:712)
at org.openapitools.client.api.WorkspaceApi.listWorkspaces(WorkspaceApi.java:694)
at org.openapitools.client.App.main(App.java:28)
,在打印消息中,显示以下内容:
{basicAuth=org.openapitools.client.auth.HttpBasicAuth@45c7e403, bearerAuth=org.openapitools.client.auth.HttpBearerAuth@2925bf5b}
I used this openapigenerator
https://openapi-generator.tech/docs/generators/java to generate a java client-app , so this create an ApiClient class, so with this class I create a request; in the example code it shows this:
import org.openapitools.client.api.WorkspaceApi;
import org.openapitools.client.model.WorkspaceReadList;
public class App {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://serveprodhost/api");
System.out.println(defaultClient.getAuthentications());
WorkspaceApi apiInstance = new WorkspaceApi(defaultClient);
try {
WorkspaceReadList result = apiInstance.listWorkspaces();
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling WorkspaceApi#listWorkspaces");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
With this code, I need to connect to a production server with a basic-auth (password and username), so how can I add these credentials to this code.
I have tried this but It didn't work, return an exception:
///// java code
System.out.println(defaultClient.getAuthentications());
HttpBasicAuth basicAuthentication = (HttpBasicAuth) defaultClient.getAuthentication("basicAuth");
basicAuthentication.setUsername("admin");
basicAuthentication.setPassword("pass");
/// exception msj
{basicAuth=org.openapitools.client.auth.HttpBasicAuth@45c7e403, bearerAuth=org.openapitools.client.auth.HttpBearerAuth@2925bf5b}
Exception when calling WorkspaceApi#listWorkspaces
Status code: 401
Reason: <html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>
Response headers: {content-length=[172], content-type=[text/html], date=[Wed, 27 Apr 2022 19:47:14 GMT], strict-transport-security=[max-age=15724800; includeSubDomains], www-authenticate=[Basic realm="Authentication Required"]}
org.openapitools.client.ApiException:
at org.openapitools.client.ApiClient.handleResponse(ApiClient.java:1081)
at org.openapitools.client.ApiClient.execute(ApiClient.java:994)
at org.openapitools.client.api.WorkspaceApi.listWorkspacesWithHttpInfo(WorkspaceApi.java:712)
at org.openapitools.client.api.WorkspaceApi.listWorkspaces(WorkspaceApi.java:694)
at org.openapitools.client.App.main(App.java:28)
and in the print message, show this:
{basicAuth=org.openapitools.client.auth.HttpBasicAuth@45c7e403, bearerAuth=org.openapitools.client.auth.HttpBearerAuth@2925bf5b}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenApi.yml:
Java:
openapi.yml:
java: