wechat

package
v0.0.0-...-22a6110 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WxAgentsConfig map[string]*WxIFAgent

Agents map

Functions

This section is empty.

Types

type WxIFAgent

type WxIFAgent struct {
	AppID     string `json:"appid"`     // Wechat Official Account App ID
	AppSecret string `json:"appsecret"` // Wechat Official Account App Securet
	Scope     string `json:"scope"`     // Scope key of 'snsapi_base' or 'snsapi_userinfo'
	IsWxApp   bool   `json:"isapp"`     // Indicate wechat app or not, true is app
}

WxIFAgent interfaces agent to using Wechat Official Account AppID and AppSecret to authenticate wechat user and get user profiles.

DESCRIPTION FROM [Wechat](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html) at 2019/07/05

There are four steps in the process for webpage authorization with wechar OAuth2.0

#### Step 1

Sending the user to the authorization page to consent to authorization, obtain code,

`http` : GET (please use https as header),
`URL`  : "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect"
`Description` : If the user agrees to authorization, the page will jump to redirect_uri/?code=CODE&state=STATE.

#### Step2

Use the code in exchange for the access_token of the webpage authorization (different from the access_token found in the basic support),

`http` : GET (please use https as header)
`URL`  : "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"
`Description` : An accurate return JSON data includes the following: {
	"access_token"  : "ACCESS_TOKEN",
	"expires_in"    : 7200,
	"refresh_token" : "REFRESH_TOKEN",
	"openid"        : "OPENID",
	"scope"         : "SCOPE"
}
Wechat will return JSON data as follows when there is an error: {
	"errcode"       : 40029,
	"errmsg"        : "invalid code"
}

#### Step 3

If necessary, the developer can refresh the webpage authorization access_token prevent it from expiring.

`http` : GET (please use https as header)
`URL`  : "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN"
`Description` : An accurate return JSON data includes the following: {
	"access_token"  : "ACCESS_TOKEN",
	"expires_in"    : 7200,
	"refresh_token" : "REFRESH_TOKEN",
	"openid"        : "OPENID",
	"scope"         : "SCOPE"
}
Wechat will return JSON data as follows when there is an error: {
	"errcode"       : 40029,
	"errmsg"        : "invalid code"
}

#### Step 4

Use the webpage authorization access_token and openid to obtain basic user information.

`http` : GET (please use https as header)
`URL`  : "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"
`Description` : An accurate return JSON data includes the following: {
    "openid"     : "OPENID",
    "nickname"   : "NICKNAME",
    "sex"        : 1,
    "province"   : "PROVINCE"
    "city"       : "CITY",
    "country"    : "COUNTRY",
    "headimgurl" : "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
    "privilege"  : ["PRIVILEGE1" "PRIVILEGE2"],
    "unionid"    : "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
Wechat will return JSON data as follows when there is an error: {
    "errcode" : 40003,
    "errmsg"  : "invalid openid"
}

`Additional` :

Testing the validity of the authorization certificate (access_token)

`http` : GET (please use https as header)
`URL`  : https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
`Description` : Accurate JSON return results: {
	"errcode" : 0, "errmsg" : "ok"
}
Example of JSON returns when there are errors: {
	"errcode" : 40003, "errmsg" : "invalid openid"
}

func (*WxIFAgent) ToWxAppTokenUrl

func (w *WxIFAgent) ToWxAppTokenUrl(requestcode string) string

`Step 2` : Bind request code and return wechat app url to get access token, please use [wx.login](https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html) get requestcode first.

see more links

- [Wechat app login follow](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html) - [Wechat app login API](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html)

func (*WxIFAgent) ToWxCodeUrl

func (w *WxIFAgent) ToWxCodeUrl(redirecturl string, state ...string) string

`Step 1` : Bind redirect url and return wechat url to get request code

func (*WxIFAgent) ToWxRefreshUrl

func (w *WxIFAgent) ToWxRefreshUrl(accesscode string) string

`Step 3` : Bind expired access toke and return wechat url to refresh it

func (*WxIFAgent) ToWxTokenUrl

func (w *WxIFAgent) ToWxTokenUrl(requestcode string) string

`Step 2` : Bind request code and return wechat url to get access token

func (*WxIFAgent) ToWxUserUrl

func (w *WxIFAgent) ToWxUserUrl(accesstoken, openid string) string

`Step 4` : Bind access token and openid, than return wechat url to get user informations

func (*WxIFAgent) ToWxVerifyUrl

func (w *WxIFAgent) ToWxVerifyUrl(accesstoken, openid string) string

`Additional` : Bind access token and openid, than return wechat url to check access token expires

type WxResult

type WxResult struct {
	ErrCode int    `json:"errcode"`
	Message string `json:"errmsg"`
}

WxResult request result return from server

type WxToken

type WxToken struct {
	AccessToken  string `json:"access_token"`
	Expires      int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	OpenID       string `json:"openid"`
	Scope        string `json:"scope"`
}

WxToken wechat access and refresh tokens

type WxUserInfo

type WxUserInfo struct {
	OpenID     string   `json:"openid"`
	Nickname   string   `json:"nickname"`
	Sex        int      `json:"sex"`
	Province   string   `json:"province"`
	City       string   `json:"city"`
	Country    string   `json:"country"`
	Headimgurl string   `json:"headimgurl"`
	Privilege  []string `json:"privilege"`
	UnionID    string   `json:"unionid"`
}

WxUserInfo wechat user informations

Jump to

Keyboard shortcuts

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