如何传递授权参数?

时间:2017-06-08 21:48:19

标签: java swagger swagger-codegen

我尝试使用swagger-codegen创建的框架,但我不了解如何将授权令牌传递给我的API方法。

E.g。我们可以使用示例Petstore:https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/android/httpclient/docs/UserApi.md#updateUser

如何将授权令牌添加到我的请求标头中(我将从之前的AuthorizationAPI请求中收到该令牌)?

也许我不了解Android的swagger-codegen的授权流程。

UserApi apiInstance = new UserApi();
User body = new User(); // User | Created user object
try {
    apiInstance.createUser(body); // here I will receive error unauhorized
} catch (ApiException e) {
    System.err.println("Exception when calling UserApi#createUser");
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

通过简要查看代码,您可以通过在UserApi https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/android/httpclient/src/main/java/io/swagger/client/api/UserApi.java#L36上调用addHeader来传递任意HTTP标头

此外,看起来swagger-codegen cli支持生成授权标头

    -a <authorization>, --auth <authorization>
        adds authorization headers when fetching the swagger definitions
        remotely. Pass in a URL-encoded string of name:header with a comma
        separating multiple values