OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[REQ] support auth token #17823

Open ramarro123 opened 9 months ago

ramarro123 commented 9 months ago

hi,

i try to generate client in java for https://gitlab.com/gitlab-org/gitlab/-/raw/master/doc/api/openapi/openapi.yaml

using java -jar openapi-generator-cli-7.3.0.jar generate -i openapi.yaml -g java --additional-properties=library=native,annotationLibrary=none

the problem is that private-token is not included anywhere so i can't add auth header

am i missing something in that? maybe the support for this kind of specs is missing?

stefan521 commented 8 months ago

Hey! You should be able to set the API key in the ApiClient.java. Something like this:

ApiClient apiClient = new ApiClient();
apiClient.setApiKey("d4ff6dfcac1f2b37113007e820fd2269")
DefaultClient defaultClient = new DefaultClient(apiClient);

Let me know if this helps. I have not tried generating the client from the spec you linked.

stefan521 commented 8 months ago

I generated the java api. Here is an example usage:

package org.openapitools;

import org.openapitools.client.ApiException;
import org.openapitools.client.api.MetadataApi;
import org.openapitools.client.model.APIEntitiesMetadata;

class ExampleUsage {

    public static void main(String[] args) throws ApiException {
        MetadataApi client = new MetadataApi();
        // Option 1
        client.getApiClient().setApiKey("api-key-here");
        // Option 2
        client.getApiClient().addDefaultHeader("Private-Token", "api-key-here");

        System.out.println("******** RESULT **********");
        APIEntitiesMetadata result = client.getApiV4Metadata();
        System.out.println(result);
        System.out.println("**************************");
    }
}