user.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package user
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/lin-jim-leon/kuaishou/util"
  6. )
  7. const (
  8. UserinfoUrl = "https://open.kuaishou.com/openapi/user_info?app_id=%s&access_token=%s"
  9. )
  10. type Info struct {
  11. Name string `json:"name"`
  12. Sex string `json:"sex"`
  13. Fan int `json:"fan"`
  14. Follow int `json:"follow"`
  15. Head string `json:"head"`
  16. BigHead string `json:"bigHead"`
  17. City string `json:"city"`
  18. }
  19. type Userresponse struct {
  20. Result int `json:"result"`
  21. ErrorMsg string `json:"error_msg"`
  22. Data Info `json:"user_info"`
  23. }
  24. // GetUserinfo 获取用户信息
  25. func GetUserinfo(appid string, accesstoken string) (info Userresponse, err error) {
  26. uri := fmt.Sprintf(UserinfoUrl, appid, accesstoken)
  27. var response []byte
  28. response, err = util.HTTPGet(uri)
  29. if err != nil {
  30. return Userresponse{}, err
  31. }
  32. var result Userresponse
  33. err = json.Unmarshal(response, &result)
  34. if err != nil {
  35. return Userresponse{}, err
  36. }
  37. if len(result.ErrorMsg) > 0 {
  38. return Userresponse{}, fmt.Errorf("GetUserinfo error: error_msg=%s", result.ErrorMsg)
  39. }
  40. return result, nil
  41. }