如何在Swagger中传递授权令牌适配PowerApps自定义连接器
解决PowerApps自定义连接器Bearer令牌传递问题
你的核心问题是Swagger定义没有正确使用Bearer认证的标准配置,而是手动添加请求头,导致请求处理异常(出现预检头Access-Control-Request-Headers: Authorization)。按以下步骤修改Swagger即可解决:
步骤1:添加Bearer认证的securityDefinitions
在Swagger顶层添加securityDefinitions,标准化定义Bearer类型的认证规则:
securityDefinitions: BearerAuth: type: apiKey name: Authorization in: header description: "格式要求:Bearer {你的令牌}"
步骤2:为需要授权的接口绑定安全规则
在需要传递令牌的接口(比如/v4_server/external/v1/maintenance/resources/properties的GET请求)中,添加security字段关联上述定义的BearerAuth,同时删除手动添加的Authorization请求头参数,还要清理接口里无效的- Bearer: []代码行。
步骤3:修正后的完整Swagger定义
swagger: '2.0' info: {version: 1.0.0, title: ACME Corp, description: ACME sample API} host: acmeapis.com basePath: / schemes: [https] consumes: [multipart/form-data] produces: [application/json] securityDefinitions: BearerAuth: type: apiKey name: Authorization in: header description: "需按格式传递: Bearer {你的令牌}" paths: /v4_server/external/v1/authenticate: post: summary: Authorize description: Used to get an authorization token operationId: Authorize parameters: - {in: formData, name: api_key, type: string, description: The API key., required: true} - {in: formData, name: client_db, type: string, description: The database to use., required: true} - {in: formData, name: username, type: string, description: The username, required: true} responses: default: description: All is well schema: type: object properties: access_token: {type: string, description: access_token} expires_in: {type: integer, format: int32, description: expires_in} refresh_token: {type: string, description: refresh_token} /v4_server/external/v1/maintenance/resources/properties: get: summary: Building/Property Location description: Building/Property Location operationId: Building_propertyLocation security: - BearerAuth: [] parameters: - {name: property_use_id, default: '2', in: query, type: string, required: true} - {name: $orderBy, default: unit, in: query, type: string, required: true} responses: '200': {description: Will send `Authenticated`} '403': {description: 'You do not have necessary permissions for the resource,'} default: description: default schema: {} /: {} definitions: {} parameters: {} responses: {} security: [] tags: []
步骤4:PowerApps连接器后续配置
导入修改后的Swagger到PowerApps自定义连接器后:
- 在连接器的安全选项卡中,选择
API Key类型,对应我们定义的BearerAuth - 输入令牌时,必须带上
Bearer前缀(例如Bearer 2b3fdha04a4d89aad9c263d5d716bcc379aff0008)
这样配置后,请求会正确携带Authorization: Bearer {token}头,不会再出现异常的预检头问题。
内容的提问来源于stack exchange,提问作者Steven
相关产品推荐
相关产品推荐

