oauth2web

package
v0.0.0-...-49a96a4 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

皆引自https://github.com/chanxuehong/wechat/blob/master/mp/user/oauth2/oauth2.go 感谢前辈

Index

Constants

View Source
const (
	Language_oauth2_zh_CN = "zh_CN" // 简体中文
	Language_oauth2_zh_TW = "zh_TW" // 繁体中文
	Language_oauth2_en    = "en"    // 英文

)
View Source
const (
	SexUnknown_oauth2 = 0 // 未知
	SexMale_oauth2    = 1 // 男性
	SexFemale_oauth2  = 2 // 女性

)
View Source
const ErrCodeOK = 0

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*OAuth2Config
	*OAuth2Token // 程序会自动更新最新的 OAuth2Token 到这个字段, 如有必要该字段可以保存起来

	HttpClient *http.Client // 如果 httpClient == nil 则默认用 http.DefaultClient

}

func (*Client) CheckAccessTokenValid

func (clt *Client) CheckAccessTokenValid() (valid bool, err error)

检验授权凭证(access_token)是否有效.

NOTE:
1. Client 需要指定 OAuth2Token
2. 先判断 err 然后再判断 valid

func (*Client) ExchangeOAuth2AccessTokenByCode

func (clt *Client) ExchangeOAuth2AccessTokenByCode(code string) (token *OAuth2Token, err error)

通过code换取网页授权access_token.

NOTE:
1. Client 需要指定 OAuth2Config
2. 如果指定了 OAuth2Token, 则会更新这个 OAuth2Token, 同时返回的也是指定的 OAuth2Token;
   否则会重新分配一个 OAuth2Token.

func (*Client) TokenRefresh

func (clt *Client) TokenRefresh() (token *OAuth2Token, err error)

刷新access_token(如果需要).

NOTE: Client 需要指定 OAuth2Config, OAuth2Token

func (*Client) UserInfo

func (clt *Client) UserInfo(lang string) (info *OAuth2UserInfo, err error)

获取用户信息(需scope为 snsapi_userinfo).

NOTE:
1. Client 需要指定 OAuth2Config, OAuth2Token
2. lang 可能的取值是 zh_CN, zh_TW, en, 如果留空 "" 则默认为 zh_CN.

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
}

func NewOAuth2Config

func NewOAuth2Config(AppId, AppSecret, RedirectURL string, Scope ...string) *OAuth2Config

func (*OAuth2Config) AuthCodeURL

func (cfg *OAuth2Config) AuthCodeURL(state string) string

请求用户授权获取code的地址.

type OAuth2Error

type OAuth2Error struct {

	// StructField 固定这个顺序, RETRY 依赖这个顺序
	ErrCode int    `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

func (*OAuth2Error) Error

func (e *OAuth2Error) Error() string

type OAuth2Token

type OAuth2Token struct {
	AccessToken  string
	RefreshToken string
	ExpiresAt    int64 // 过期时间, unixtime, 分布式系统要求时间同步, 建议使用 NTP

	OpenId  string
	UnionId string   // UnionID机制
	Scopes  []string // 用户授权的作用域

}

用户相关的 oauth2 token 信息

type OAuth2UserInfo

type OAuth2UserInfo struct {
	OpenId   string `json:"openid"`   // 用户的唯一标识
	Nickname string `json:"nickname"` // 用户昵称
	Sex      int    `json:"sex"`      // 用户的性别,值为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"`
}

Jump to

Keyboard shortcuts

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