【问题标题】:How to pass authorization parameters?如何传递授权参数?
【发布时间】:2017-06-08 21:48:19
【问题描述】:

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

例如我们可以使用示例 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();
}

【问题讨论】:

    标签: java swagger swagger-codegen


    【解决方案1】:

    通过简要查看代码,您似乎可以通过调用 addHeaderon 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 来传递任意 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
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2015-09-16
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多