|
@@ -690,7 +690,7 @@ func (s *Server) getZaloUserInfo(code, codeVerifier string) (*ZaloUser, error) {
|
|
|
|
|
|
|
|
var tokenResp struct {
|
|
var tokenResp struct {
|
|
|
AccessToken string `json:"access_token"`
|
|
AccessToken string `json:"access_token"`
|
|
|
- ExpiresIn int `json:"expires_in"`
|
|
|
|
|
|
|
+ ExpiresIn string `json:"expires_in"` // Zalo 返回的是字符串类型
|
|
|
Error int `json:"error"`
|
|
Error int `json:"error"`
|
|
|
Message string `json:"message"`
|
|
Message string `json:"message"`
|
|
|
}
|
|
}
|
|
@@ -715,6 +715,16 @@ func (s *Server) getZaloUserInfo(code, codeVerifier string) (*ZaloUser, error) {
|
|
|
userBody, _ := io.ReadAll(userResp.Body)
|
|
userBody, _ := io.ReadAll(userResp.Body)
|
|
|
fmt.Printf("Zalo user response: %s\n", string(userBody))
|
|
fmt.Printf("Zalo user response: %s\n", string(userBody))
|
|
|
|
|
|
|
|
|
|
+ // Zalo API 错误响应格式: {"error": -501, "message": "..."}
|
|
|
|
|
+ // Zalo API 成功响应格式: {"id": "...", "name": "...", "picture": {...}}
|
|
|
|
|
+ var errorResp struct {
|
|
|
|
|
+ Error int `json:"error"`
|
|
|
|
|
+ Message string `json:"message"`
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := json.Unmarshal(userBody, &errorResp); err == nil && errorResp.Error != 0 {
|
|
|
|
|
+ return nil, fmt.Errorf("get user info failed: [%d] %s", errorResp.Error, errorResp.Message)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
var userInfo struct {
|
|
var userInfo struct {
|
|
|
ID string `json:"id"`
|
|
ID string `json:"id"`
|
|
|
Name string `json:"name"`
|
|
Name string `json:"name"`
|
|
@@ -723,16 +733,12 @@ func (s *Server) getZaloUserInfo(code, codeVerifier string) (*ZaloUser, error) {
|
|
|
URL string `json:"url"`
|
|
URL string `json:"url"`
|
|
|
} `json:"data"`
|
|
} `json:"data"`
|
|
|
} `json:"picture"`
|
|
} `json:"picture"`
|
|
|
- Error struct {
|
|
|
|
|
- Code int `json:"code"`
|
|
|
|
|
- Message string `json:"message"`
|
|
|
|
|
- } `json:"error"`
|
|
|
|
|
}
|
|
}
|
|
|
if err := json.Unmarshal(userBody, &userInfo); err != nil {
|
|
if err := json.Unmarshal(userBody, &userInfo); err != nil {
|
|
|
return nil, fmt.Errorf("parse user info failed: %v", err)
|
|
return nil, fmt.Errorf("parse user info failed: %v", err)
|
|
|
}
|
|
}
|
|
|
- if userInfo.Error.Code != 0 {
|
|
|
|
|
- return nil, fmt.Errorf("get user info failed: %s", userInfo.Error.Message)
|
|
|
|
|
|
|
+ if userInfo.ID == "" {
|
|
|
|
|
+ return nil, fmt.Errorf("get user info failed: empty user id")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return &ZaloUser{
|
|
return &ZaloUser{
|