【发布时间】:2021-11-22 06:02:46
【问题描述】:
在开发 Oauth 授权中,我遇到了绑定问题
var googleOauthConfig = &model.OauthConfig{
Client_id: os.Getenv("webclient_id"),
Client_secret: "",
Redirect_uri: info.GoogleRedirectPath,
Grant_type: "authorization_code",
}
const RequestGoogleToken string = "https://accounts.google.com/o/oauth2/v2/auth"
type Token struct {
Access_token string `json:"access_token" binding:"required"`
Token_type string `json:"token_type" binding:"required"`
Expiration time.Time `json:"expires_in" binding:"required"`
Refresh_token string `json:"refresh_token" binding:"required"`
}
我必须在 RequestGoogleToken URI 中请求方法“POST”并获取令牌结构数据
我可以用这种方式
token := &model.Token{}
pbytes, _ := json.Marshal(token)
buff := bytes.NewBuffer(pbytes)
resp, err := http.Post(info.RequestGoogleToken, "application/json", buff)
我使用了 http 函数“POST”,我可以获取 []byte 的数据类型 我无法绑定 Token Struct
我想要 gin framework 方法,如果你提出任何想法,我将不胜感激
【问题讨论】:
-
Gin 是服务器库,而不是客户端库。只需使用
encoding/json解组数据。 -
谢谢!!我用过 json.Unmarshal 我可以处理它