Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAPI 中 SetAuthScheme 未生效 #56

Open
sslime336 opened this issue Jan 26, 2025 · 0 comments
Open

OpenAPI 中 SetAuthScheme 未生效 #56

sslime336 opened this issue Jan 26, 2025 · 0 comments

Comments

@sslime336
Copy link

sslime336 commented Jan 26, 2025

背景

个人 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant