auth

package
v0.0.0-...-f32fcc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthUrl                = "" /* 127-byte string literal not displayed */
	AuthAccessTokenUrl     = " https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"
	RefreshAccessTokenUrl  = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"
	GetUserInfoUrl         = "https://api.weixin.qq.com/sns/userinfo?access_token=&s&openid=%s&lang=%s"
	ValidateAccessTokenUrl = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s"
)

Variables

This section is empty.

Functions

func GetAuthUrl

func GetAuthUrl(appid, redirect_uri, response_type string, OnlyOpenID bool, state string) string

appid 公众号的唯一标识 redirect_uri 授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理 response_type 返回类型,请填写code scope 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),

snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。
并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )

state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 微信会对授权链接做正则强匹配校验,如果链接的参数顺序不对,授权页面将无法正常访问

func ValidateAccessToken

func ValidateAccessToken(accessToken, openID string) error

Types

type AuthInfo

type AuthInfo struct {
	AccessToken  string `json:"access_token"`  //网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
	ExpireIn     int    `json:"expires_in"`    //access_token接口调用凭证超时时间,单位(秒)
	RefreshToken string `json:"refresh_token"` //用户刷新access_token
	OpenID       string `json:"openid"`        //用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
	Scope        string `json:"scope"`         //用户授权的作用域,使用逗号(,)分隔
}

func GetAuthAccessToken

func GetAuthAccessToken(appid, secret, code string) (*AuthInfo, error)

code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。

func RefreshAuthAccessToken

func RefreshAuthAccessToken(appid, refreshToken string) (*AuthInfo, error)

由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新, refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。

type WxUserInfo

type WxUserInfo struct {
	OpenID    string `json:"openid"`     //用户的唯一标识
	NickName  string `json:"nickname"`   //用户昵称
	Sex       int    `json:"sex"`        //用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
	Province  string `json:"province"`   //用户个人资料填写的省份
	City      string `json:"city"`       //普通用户个人资料填写的城市
	Country   string `json:"country"`    //国家,如中国为CN
	HeadImage string `json:"headimgurl"` //用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),
	// 用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
	Privilege []string `json:"privilege"` //用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
	UnionID   string   `json:"unionid"`   //只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
}

用户授权获得的用户信息

func GetUserInfo

func GetUserInfo(accessToken, openID, lang string) (*WxUserInfo, error)

lang为语言,zh_CN 简体,zh_TW 繁体,en 英语

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL