oauth2

package
v0.0.0-...-5652a87 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2017 License: Apache-2.0 Imports: 9 Imported by: 0

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 {
	Config Config

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

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

func (*Client) Exchange

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

通过code换取网页授权access_token.

返回的 token == clt.Token

func (*Client) GetUserInfo

func (clt *Client) GetUserInfo(userinfo interface{}, lang string) (err error)

获取用户信息.

lang 可以为空值.

func (*Client) TokenRefresh

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

刷新access_token(如果需要).

返回的 token == clt.Token

type Config

type Config interface {
	AuthCodeURL(state string, redirectURIExt url.Values) string // 请求用户授权的地址, 获取code; redirectURIExt 用于扩展回调地址的参数
	ExchangeTokenURL(code string) string                        // 通过code换取access_token的地址
	RefreshTokenURL(refreshToken string) string                 // 刷新access_token的地址
	UserInfoURL(accessToken, openId, lang string) string        // 获取用户信息的地址, lang可以为空值
}

type Error

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

func (*Error) Error

func (e *Error) Error() string

type Token

type Token struct {
	AccessToken  string `json:"access_token"`
	CreateAt     int64  `json:"create_at"`  // 创建时间, unixtime, 分布式系统要求时间同步, 建议使用 NTP
	ExpiresIn    int64  `json:"expires_in"` // 超时时间, seconds
	RefreshToken string `json:"refresh_token"`

	OpenId  string   `json:"openid"`
	UnionId string   `json:"unionid,omitempty"`
	Scopes  []string `json:"scopes,omitempty"` // 用户授权的作用域
}

func (*Token) AccessTokenExpired

func (token *Token) AccessTokenExpired() bool

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

type TokenStorage

type TokenStorage interface {
	Get() (*Token, error)
	Put(*Token) error
}

Jump to

Keyboard shortcuts

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