【问题标题】:How to add Bearer token to headers of generated api endpoint from openapi-generator-cli如何将 Bearer 令牌添加到从 openapi-generator-cli 生成的 api 端点的标头
【发布时间】:2020-11-10 21:24:08
【问题描述】:

我正在测试 openapi-generator-cli 以从 swagger.json 文件为 React 客户端应用程序生成服务。

现在一些路由受到保护,并且在标头中需要 Bearer token。登录后返回token。

在调用受保护路由的生成方法时,是否可以将它们添加为配置或标头参数?

【问题讨论】:

    标签: reactjs typescript swagger bearer-token openapi-generator


    【解决方案1】:

    在我的组件中,我正在使用将配置作为参数的服务来实现 Api。

    myToken 是我之前存储令牌的局部变量。

    import * as Api from '../../api/src';
    const configParameters: Api.ConfigurationParameters = {
      headers: {
        'Authorization': 'Bearer ' + myToken,
      },
    };
    const configuration = new Api.Configuration(configParameters);
    const myService = new Api.MyApi(configuration); 
    

    【讨论】:

    • 应该解释你的答案,而不是仅仅转储代码
    • @DᴀʀᴛʜVᴀᴅᴇʀ 你需要什么解释?
    猜你喜欢
    • 1970-01-01
    • 2022-07-23
    • 2021-12-27
    • 2021-11-15
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2019-12-07
    • 2022-12-20
    相关资源
    最近更新 更多