如何正确读取 golang oauth2 的错误
How to properly read errors from golang oauth2
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
fmt.Fprintf(w, "Err: %+v", err)
}
fprintf 的输出是:
Err: oauth2: cannot fetch token: 401 Unauthorized
Response: {"error":"code_already_used","error_description":"code_already_used"}
我想检查是否 "error" = "code_already_used"。对于我的生活,我无法弄清楚如何。
我如何check/return/read"error"或"error_description"的错误?
我看了oauth2的代码,有点偏题了
// retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
// This token is then mapped from *internal.Token into an *oauth2.Token which is returned along
// with an error..
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) {
tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.Endpoint.TokenURL, v)
if err != nil {
if rErr, ok := err.(*internal.RetrieveError); ok {
return nil, (*RetrieveError)(rErr)
}
return nil, err
}
return tokenFromInternal(tk), nil
}
怎么猜我是在尝试查看 (*RetrieveError) 部分。对吗?
谢谢!
表达式:
(*RetrieveError)(rErr)
将 rErr
的类型从 *internal.RetrieveError
转换为 *RetrieveError
。由于 RetrieveError
是在 oauth2
包中声明的,您可以 键入断言 您收到的错误 *oauth2.RetrieveError
以获取详细信息。详细信息作为字节片段包含在该类型的 Body 字段中。
由于一段字节不是要检查的最佳格式,并且在您的情况下,字节似乎包含一个 json 对象,您可以通过预定义可以解组的类型来简化您的生活这些细节。
即:
type ErrorDetails struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
fmt.Fprintf(w, "Err: %+v", err)
if rErr, ok := err.(*oauth2.RetrieveError); ok {
details := new(ErrorDetails)
if err := json.Unmarshal(rErr.Body, details); err != nil {
panic(err)
}
fmt.Println(details.Error, details.ErrorDescription)
}
}
可以这样做。
arr := strings.Split(err.Error(), "\n")
str := strings.Replace(arr[1], "Response: ", "", 1)
var details ErrorDetails
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(str), &details)
if err == nil {
beego.Debug(details.Error)
beego.Debug(details.ErrorDescription)
}
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
fmt.Fprintf(w, "Err: %+v", err)
}
fprintf 的输出是:
Err: oauth2: cannot fetch token: 401 Unauthorized
Response: {"error":"code_already_used","error_description":"code_already_used"}
我想检查是否 "error" = "code_already_used"。对于我的生活,我无法弄清楚如何。
我如何check/return/read"error"或"error_description"的错误?
我看了oauth2的代码,有点偏题了
// retrieveToken takes a *Config and uses that to retrieve an *internal.Token.
// This token is then mapped from *internal.Token into an *oauth2.Token which is returned along
// with an error..
func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) {
tk, err := internal.RetrieveToken(ctx, c.ClientID, c.ClientSecret, c.Endpoint.TokenURL, v)
if err != nil {
if rErr, ok := err.(*internal.RetrieveError); ok {
return nil, (*RetrieveError)(rErr)
}
return nil, err
}
return tokenFromInternal(tk), nil
}
怎么猜我是在尝试查看 (*RetrieveError) 部分。对吗?
谢谢!
表达式:
(*RetrieveError)(rErr)
将 rErr
的类型从 *internal.RetrieveError
转换为 *RetrieveError
。由于 RetrieveError
是在 oauth2
包中声明的,您可以 键入断言 您收到的错误 *oauth2.RetrieveError
以获取详细信息。详细信息作为字节片段包含在该类型的 Body 字段中。
由于一段字节不是要检查的最佳格式,并且在您的情况下,字节似乎包含一个 json 对象,您可以通过预定义可以解组的类型来简化您的生活这些细节。
即:
type ErrorDetails struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
fmt.Fprintf(w, "Err: %+v", err)
if rErr, ok := err.(*oauth2.RetrieveError); ok {
details := new(ErrorDetails)
if err := json.Unmarshal(rErr.Body, details); err != nil {
panic(err)
}
fmt.Println(details.Error, details.ErrorDescription)
}
}
可以这样做。
arr := strings.Split(err.Error(), "\n")
str := strings.Replace(arr[1], "Response: ", "", 1)
var details ErrorDetails
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal([]byte(str), &details)
if err == nil {
beego.Debug(details.Error)
beego.Debug(details.ErrorDescription)
}