|
|
package qq
import ( "encoding/json" "fmt" "github.com/ouxuanserver/osmanthuswine/src/helper" "io/ioutil" "net/http" "net/url" )
var ( Appid = "" AppKey = "" RedirectUri = "" DefaultScope = "get_user_info" CodeUrl = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&scope=%s" TokenUrl = "https://graph.qq.com/oauth2.0/authorize?grant_type=authorization_code&client_id=%s&client_serect=%s&code=%s&redirect_uri=%s" RefreshUrl = "https://graph.qq.com/oauth2.0/token?grant_type=refresh_token&client_id=%s&client_serect=%s&refresh_token=%s" PcOpenidUrl = "https://graph.qq.com/oauth2.0/me?access_token=%s" WapOpenidUrl = "https://graph.z.qq.com/moc2/me?access_token=%s" UserInfoUrl = "https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s&format=json" )
func GetCodeUrl() string { uid := helper.CreateUUID() return fmt.Sprintf(CodeUrl, Appid, url.PathEscape(RedirectUri), uid, DefaultScope) }
func GetTokenUrl(code string) string { return fmt.Sprintf(TokenUrl, Appid, AppKey, code, RedirectUri) }
func GetRefreshUrl(refreshToken string) string { return fmt.Sprintf(RefreshUrl, Appid, AppKey, refreshToken) }
func GetPcOpenidUrl(accessToken string) string { return fmt.Sprintf(PcOpenidUrl, accessToken) }
func GetWapOpenidUrl(accessToken string) string { return fmt.Sprintf(WapOpenidUrl, accessToken) }
func GetUserInfoUrl(accessToken, openid string) string { return fmt.Sprintf(UserInfoUrl, accessToken, Appid, openid) }
type Token struct { AccessToken string `json:"access_token"` ExpiresIn int64 `json:"expires_in"` RefreshToken string `json:"refresh_token"` }
func GetToken(code string) (*Token, error) { tokenUrl := GetTokenUrl(code) resp, err := http.Get(tokenUrl) if err != nil { return nil, err } body, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return nil, err } token := new(Token) err = json.Unmarshal(body, token) return token, err }
func RefreshToken(refreshToken string) (*Token, error) { refreshUrl := GetRefreshUrl(refreshToken) resp, err := http.Get(refreshUrl) if err != nil { return nil, err } body, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return nil, err } token := new(Token) err = json.Unmarshal(body, token) return token, err }
type Open struct { Openid string `json:"openid"` ClientId string `json:"client_id"` }
func GetPcOpenid(accessToken string) (*Open, error) { pcOpenidUrl := GetPcOpenidUrl(accessToken) resp, err := http.Get(pcOpenidUrl) if err != nil { return nil, err } body, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return nil, err } open := new(Open) err = json.Unmarshal(body, open) return open, err }
type UserInfo struct { Ret int `json:"ret"` Msg string `json:"msg"` Nickname string `json:"nickname"` Figureurl string `json:"figureurl"` Figureurl1 string `json:"figureurl_1"` Figureurl2 string `json:"figureurl_2"` FigureurlQq1 string `json:"figureurl_qq_1"` FigureurlQq2 string `json:"figureurl_qq_2"` Gender string `json:"gender"` IsYellowVip string `json:"is_yellow_vip"` Vip string `json:"vip"` YellowVipLevel string `json:"yellow_vip_level"` Level string `json:"level"` IsYellowYearVip string `json:"is_yellow_year_vip"` }
func GetUserInfo(accessToken, openid string) (*UserInfo, error) { userInfoUrl := GetUserInfoUrl(accessToken, openid) resp, err := http.Get(userInfoUrl) if err != nil { return nil, err } body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } userInfo := new(UserInfo) err = json.Unmarshal(body, userInfo) return userInfo, err }
|