oauth2

package
v0.0.0-...-36f0325 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: Apache-2.0 Imports: 7 Imported by: 91

README

微信 oauth2 基础库

Documentation

Overview

微信 oauth2 基础库

Index

Constants

View Source
const (
	ErrCodeOK = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Endpoint Endpoint

	// TokenStorage 和 Token 两个字段正常情况下只用指定一个, 如果两个同时被指定了, 优先使用 TokenStorage
	TokenStorage TokenStorage
	Token        *Token // Client 自动将最新的 Token 更新到此字段, 不管 Token 字段一开始是否被指定!!!

	HttpClient *http.Client // 如果 HttpClient == nil 则默认用 util.DefaultHttpClient
}

func (*Client) ExchangeToken

func (clt *Client) ExchangeToken(code string) (token *Token, err error)

ExchangeToken 通过 code 换取网页授权 access_token.

NOTE: 返回的 token == clt.Token

func (*Client) GetToken

func (clt *Client) GetToken(autoRefresh bool) (tk *Token, err error)

GetToken 获取 Token, autoRefresh 为 true 时如果 Token 过期则自动刷新.

func (*Client) RefreshToken

func (clt *Client) RefreshToken(refreshToken string) (token *Token, err error)

RefreshToken 刷新 access_token.

NOTE:
1. refreshToken 可以为空.
2. 返回的 token == clt.Token

type Endpoint

type Endpoint interface {
	ExchangeTokenURL(code string) string        // 通过code换取access_token的地址
	RefreshTokenURL(refreshToken string) string // 刷新access_token的地址
}

type Error

type Error struct {
	ErrCode int64  `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

func (*Error) Error

func (err *Error) Error() string

type Token

type Token struct {
	AccessToken  string `json:"access_token"`            // 网页授权接口调用凭证
	CreatedAt    int64  `json:"created_at"`              // access_token 创建时间, unixtime, 分布式系统要求时间同步, 建议使用 NTP
	ExpiresIn    int64  `json:"expires_in"`              // access_token 接口调用凭证超时时间, 单位: 秒
	RefreshToken string `json:"refresh_token,omitempty"` // 刷新 access_token 的凭证

	OpenId  string `json:"openid,omitempty"`
	UnionId string `json:"unionid,omitempty"`
	Scope   string `json:"scope,omitempty"` // 用户授权的作用域, 使用逗号(,)分隔
}

func (*Token) Expired

func (token *Token) Expired() bool

Expired 判断 token.AccessToken 是否过期, 过期返回 true, 否则返回 false.

type TokenStorage

type TokenStorage interface {
	Token() (*Token, error)
	PutToken(*Token) error
}

Jump to

Keyboard shortcuts

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