cancel
Showing results for 
Search instead for 
Did you mean: 

swagger-codegen-maven-plugin: How do I use the Bearer token returned after login?

tarekz
Visitor

swagger-codegen-maven-plugin: How do I use the Bearer token returned after login?

NOTE: The issue was I used the wrong header "Authentication" instead of "Authorization". Please close!

 

I am using swagger-codegen-maven-plugin to generate java code to use in api tests.

swagger-codegen-maven-plugin reads an inputSpec from swagger/docs/v1.

In there you find the folowing entry:

"securityDefinitions":{"Token":{"type":"apiKey","description":"API Key Authentication (bearer token)","name":"Authorization","in":"header"}}}

This generates an ApiClient.java class with the following constructor:

public ApiClient() {
httpClient = new OkHttpClient();


verifyingSsl = true;

json = new JSON();

// Set default User-Agent.
setUserAgent("Swagger-Codegen/1.0.0/java");

// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
authentications.put("Token", new ApiKeyAuth("header", "Authorization"));
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}

 

 So far I have tried to do the following:

accountsApi.getApiClient().setApiKey(loginResponse.getAccessToken());

 And

accountsApi.getApiClient().addDefaultHeader("Authentication", "Bearer " + loginResponse.getAccessToken());

But I keep getting an error that the request is not authorized. 

[05/24/2019 13:57:07:748 EDT] INFO SwaggerClientConfiguration: {"Message":"Authorization has been denied for this request."}

[05/24/2019 13:57:07:748 EDT] INFO SwaggerClientConfiguration: <-- END HTTP (61-byte body)

 

What am I doing wrong?

 

Thanks in advance

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors