Skip to content

OpenAPI 中 SetAuthScheme 未生效 #56

Description

@sslime336

背景

个人 bot 更新依赖做 webhook 迁移变更时原有接口出现鉴权问题

2025-01-26T14:58:50.846+0800    INFO    v1/openapi.go:113       [OPENAPI]POST https://sandbox.api.sgroup.qq.com/v2/groups/xxx/messages, traceID:xxx, status:401 Unauthorized, elapsed:45.28308ms req: {"content":"xxx","msg_id":"ROBOT1.0_ixx-","msg_seq":123}, resp: {"message":"请求头Authorization参数格式错误","code":11241,"err_code":40011005,"trace_id":"xxx"}
2025-01-26T14:58:50.846+0800    ERROR   handler/handler.go:19   handle payload error    {"error": "code:401, text:{\"message\":\"请求头Authorization参数格式错误\",\"code\":11241,\"err_code\":40011005,\"trace_id\":\"xxx\"}, traceID:xxx"}

复现

基于当前所依赖版本 resty-go: github.com/go-resty/resty/v2 v2.16.5

func TestRestySetAuthSchema(t *testing.T) {
	client := resty.New().
		SetDebug(true).
		OnBeforeRequest(
			func(c *resty.Client, _ *resty.Request) error {
				c.SetAuthScheme("QQBot")
				c.SetAuthToken("KLadnbiraenibi2uc3rh9pwng")
				return nil
			},
		)

	client.R().Post("localhost:9090")
}

Output:

=== RUN   TestRestySetAuthSchema
2025/01/26 16:13:29.440023 DEBUG RESTY
==============================================================================
~~~ REQUEST ~~~
POST  9090  HTTP/1.1
HOST   :
HEADERS:
        Authorization: Bearer KLadnbiraenibi2uc3rh9pwng
        User-Agent: go-resty/2.16.5 (https://github.com/go-resty/resty)
BODY   :
***** NO CONTENT *****
------------------------------------------------------------------------------
~~~ RESPONSE ~~~
STATUS       :
PROTO        :
RECEIVED AT  : 2025-01-26T16:13:29.3983287+08:00
TIME DURATION: 540µs
HEADERS      :

BODY         :
***** NO CONTENT *****
==============================================================================

可能的修复方法:

func TestRestySetAuthSchema(t *testing.T) {
	client := resty.New().
		SetDebug(true).
		OnBeforeRequest(
			func(c *resty.Client, r *resty.Request) error {
-			        c.SetAuthScheme("QQBot")
+                               r.SetAuthScheme("QQBot")
				c.SetAuthToken("KLadnbiraenibi2uc3rh9pwng")
				return nil
			},
		)

	client.R().Post("localhost:9090")
}

Output:

2025/01/26 16:17:35.746922 DEBUG RESTY
==============================================================================
~~~ REQUEST ~~~
POST  9090  HTTP/1.1
HOST   :
HEADERS:
        Authorization: QQBot KLadnbiraenibi2uc3rh9pwng
        User-Agent: go-resty/2.16.5 (https://github.com/go-resty/resty)
BODY   :
***** NO CONTENT *****
------------------------------------------------------------------------------
~~~ RESPONSE ~~~
STATUS       :
PROTO        :
RECEIVED AT  : 2025-01-26T16:17:35.6975277+08:00
TIME DURATION: 0s
HEADERS      :

BODY         :
***** NO CONTENT *****
==============================================================================

上游 issue

go-resty/resty#965

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions