Documentation ¶
Overview ¶
封装网页授权获取用户基本信息的支持.
Index ¶
Constants ¶
View Source
const ( Language_zh_CN = "zh_CN" // 简体中文 Language_zh_TW = "zh_TW" // 繁体中文 Language_en = "en" // 英文 )
View Source
const ( SEX_UNKNOWN = 0 // 未知 SEX_MALE = 1 // 男性 SEX_FEMALE = 2 // 女性 )
Variables ¶
This section is empty.
Functions ¶
func HeadImageSize ¶
获取用户图像的大小
@headImageURL: 用户头像URL,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像) NOTE: 请确保 headImageURL 不为空
Types ¶
type Client ¶
type Client struct { *OAuth2Config // 下面两个字段必须指定其中一个(通过code换取网页授权access_token 时除外) // 如果指定 OAuth2Token 则表示手动管理 OAuth2Token, 需要手动更新 OAuth2Token // 如果指定 TokenCache 则表示自动管理 OAuth2Token, 程序会自动更新 OAuth2Token *OAuth2Token TokenCache TokenCache // 如果 httpClient == nil 则默认用 http.DefaultClient, // see ../CommonHttpClient 和 ../MediaHttpClient. HttpClient *http.Client }
func (*Client) CheckAccessTokenValid ¶
检查 access_token 是否有效
func (*Client) Exchange ¶
func (c *Client) Exchange(code string) (token *OAuth2Token, err error)
通过code换取网页授权 access_token
NOTE: 如果成功 c.OAuth2Token 字段也会得到更新, 如果指定了 TokenCache, TokenCache.PutToken 也会被调用
func (*Client) TokenRefresh ¶
func (c *Client) TokenRefresh() (token *OAuth2Token, err error)
刷新access_token(如果需要)
NOTE: 如果成功 c.OAuth2Token 字段也会得到更新, 如果指定了 TokenCache, TokenCache.PutToken 也会被调用
type OAuth2Config ¶
type OAuth2Config struct { AppId string AppSecret string // 应用授权作用域,拥有多个作用域用逗号(,)分隔; // 目前有 snsapi_base, snsapi_userinfo. Scope string // 用户授权后跳转的目的地址 // 用户授权后跳转到 RedirectURL?code=CODE&state=STATE // 用户禁止授权跳转到 RedirectURL?state=STATE RedirectURL string }
oauth2 相关配置 ( 一般全局只用保存一个变量 )
func NewOAuth2Config ¶
func NewOAuth2Config(appid, appsecret, redirectURL string, scope ...string) *OAuth2Config
func (*OAuth2Config) AuthCodeURL ¶
func (cfg *OAuth2Config) AuthCodeURL(state string) string
请求用户授权时跳转的地址.
type OAuth2Token ¶
type OAuth2Token struct { AccessToken string RefreshToken string ExpiresAt int64 // unixtime OpenId string Scopes []string // 用户授权的作用域 }
用户相关的 oauth2 token 信息
NOTE: 每个用户对应一个这样的结构, 应该缓存起来, 一般缓存在 session 中.
type TokenCache ¶
type TokenCache interface { Token() (*OAuth2Token, error) PutToken(*OAuth2Token) error }
OAuth2Token 缓存接口
type UserInfo ¶
type UserInfo struct { OpenId string `json:"openid"` // 用户的唯一标识 Nickname string `json:"nickname"` // 用户昵称 Sex int `json:"sex,string"` // 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知 City string `json:"city"` // 普通用户个人资料填写的城市 Province string `json:"province"` // 用户个人资料填写的省份 Country string `json:"country"` // 国家,如中国为CN // 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像), // 用户没有头像时该项为空 HeadImageURL string `json:"headimgurl,omitempty"` // 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom) Privilege []string `json:"privilege"` // 用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。 UnionId string `json:"unionid"` }
Click to show internal directories.
Click to hide internal directories.