swagger-api / swagger-api/swagger-codegen

Java Swagger generated rest client Problem authorizing requests via the generated client.

Open
#8,606 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Hello ,

I am getting error in java client as below while using Basic Authentication in Swagger 2.0 version.
while generating client (java client) , we used swagger-codegen-cli-2.1.2.jar version.

io.swagger.client.ApiException: Authentication Required
at io.swagger.client.ApiClient.invokeAPI(ApiClient.java:445)
at io.swagger.client.api.CustomersApi.customersGet(CustomersApi.java:97)

from java level i can generate token by username:password.

token is matching between swagger UI tool and Java client.

below is the sample code
String auth = key + ":" + secret;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
System.out.println("the basic auth header value is "+authHeader);

i am not sure how to pass this token in request header.
kindly help me , how to attach generated token in request header by using Generated Java Client API Classes?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the generated client flow in ApiClient.java around invokeAPI and the call from CustomersApi.java. Reproduce the Basic Authentication request with swagger-codegen-cli-2.1.2.jar and compare it with the working Swagger UI request. Done means the generated Java client sends the expected authorization header and the request no longer returns Authentication Required.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.