socialite

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 6 Imported by: 0

README

go-socialite

oauth2授权登录(QQ、Wchat、Weibo)

Build Status Go Report Card codecov

引入方式

go get github.com/birjemin/go-socialite

使用方式

  • 初始化
var (
    httpClient = &utils.HTTPClient{
        Client: &http.Client{
            Timeout: 5 * time.Second,
        },
    }

    defaultObj = &socialite.Default{}

    qqObj = &socialite.Qq{
        AppID:       "",
        AppSecret:   "",
        RedirectURL: "https://domain/qq/callback",
        HTTPRequest: httpClient,
    }

    wxObj = &socialite.Wechat{
        AppID:       "",
        AppSecret:   "",
        RedirectURL: "https://domain/qq/callback",
        HTTPRequest: httpClient,
    }

    wbObj = &socialite.Weibo{
        ClientID:     "",
        ClientSecret: "",
        RedirectURL:  "http://domain.com/wb/callback",
        HTTPRequest:  httpClient,
    }
)

func dispatch(platform string) socialite.ISocialite {
    var obj socialite.ISocialite

    switch platform {
    case "qq":
        obj = qqObj
    case "wx":
        obj = wxObj
    case "wb":
        obj = wbObj
    default:
        obj = defaultObj
    }

    return obj
}
obj := dispatch("wx")
// obj := dispatch("wb")
// obj := dispatch("qq")

  • 获取授权地址(登录完成之后会带上CODE跳转到回调地址中)
log.Print("authorize_url: ", obj.GetAuthorizeURL())
  • 获取授权AccessToken()
// 上一步得到的CODE
resp, err := obj.Token("CODE")
// 断言
ret, ok := resp.(*socialite.WxRespToken)
if ok {
    log.Printf("ret: %#v", ret)
}
  • 获取用户的OPEN_ID(qq接口专有,wechat、weibo在上一步中已经返回用户标识)
resp, err := obj.GetMe("ACCESS_TOKEN")
// 断言
ret, ok := resp.(*socialite.QqRespMe)
if ok {
    log.Printf("ret: %#v", ret)
}
  • 获取用户信息
resp, err := obj.GetUserInfo("ACCESS_TOKEN", "OPEN_ID")
// 断言
ret, ok := resp.(*socialite.WxUserInfo)
if ok {
    log.Printf("ret: %#v", ret)
}

测试

  • 测试
    go test
    
  • 格式化代码
    golint
    
  • 覆盖率
    go test -cover
    go test -coverprofile=coverage.out 
    go tool cover -html=coverage.out
    

备注

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Default

type Default struct {
}

Default struct

func (*Default) GetAuthorizeURL

func (d *Default) GetAuthorizeURL(args ...string) string

GetAuthorizeURL get authorize url

func (*Default) GetMe

func (d *Default) GetMe(accessToken string) (interface{}, error)

GetMe get me

func (*Default) GetUserInfo

func (d *Default) GetUserInfo(accessToken, openID string) (interface{}, error)

GetUserInfo get user info

func (*Default) RefreshToken

func (d *Default) RefreshToken(refreshToken string) (interface{}, error)

RefreshToken refresh token

func (*Default) Token

func (d *Default) Token(code string) (interface{}, error)

Token token

type ISocialite

type ISocialite interface {
	// GetAuthorizeURL get authorize url
	GetAuthorizeURL(args ...string) string

	// Token get token
	Token(code string) (interface{}, error)

	// RefreshToken refresh token
	RefreshToken(refreshToken string) (interface{}, error)

	// GetMe get open_id if it needs necessarily
	GetMe(accessToken string) (interface{}, error)

	// GetUserInfo get user info
	GetUserInfo(accessToken, openID string) (interface{}, error)
}

ISocialite interface

type Qq

type Qq struct {
	AppID       string
	AppSecret   string
	RedirectURL string
	HTTPRequest *utils.HTTPClient
}

Qq struct @doc: https://wiki.open.qq.com/wiki/website/%E4%BD%BF%E7%94%A8Authorization_Code%E8%8E%B7%E5%8F%96Access_Token

func (*Qq) GetAuthorizeURL

func (q *Qq) GetAuthorizeURL(args ...string) string

GetAuthorizeURL get authorize url

func (*Qq) GetMe

func (q *Qq) GetMe(accessToken string) (interface{}, error)

GetMe get me

func (*Qq) GetUserInfo

func (q *Qq) GetUserInfo(accessToken, openID string) (interface{}, error)

GetUserInfo get user info

func (*Qq) RefreshToken

func (q *Qq) RefreshToken(refreshToken string) (interface{}, error)

RefreshToken refresh token

func (*Qq) Token

func (q *Qq) Token(code string) (interface{}, error)

Token get token

type QqRespMe

type QqRespMe struct {
	ClientID string `json:"client_id"`
	OpenID   string `json:"openid"`
	// contains filtered or unexported fields
}

QqRespMe response of me

type QqRespToken

type QqRespToken struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	// contains filtered or unexported fields
}

QqRespToken struct

type QqRespUserInfo

type QqRespUserInfo struct {
	Ret              int    `json:"ret"`
	Msg              string `json:"msg"`
	IsLost           int    `json:"is_lost"`
	Nickname         string `json:"nickname"`
	Gender           string `json:"gender"`
	GenderType       int    `json:"gender_type"`
	Province         string `json:"province"`
	City             string `json:"city"`
	Year             string `json:"year"`
	Constellation    string `json:"constellation"`
	FigureURL        string `json:"figureurl"`
	FigureURL1       string `json:"figureurl_1"`
	FigureURL2       string `json:"figureurl_2"`
	FigureQqURL      string `json:"figureurl_qq"`
	FigureQqURL1     string `json:"figureurl_qq_1"`
	FigureQqURL2     string `json:"figureurl_qq_2"`
	FigureURLType    string `json:"figureurl_type"`
	IsYellowVIP      string `json:"is_yellow_vip"`
	VIP              string `json:"vip"`
	YellowVIPLevel   string `json:"yellow_vip_level"`
	Level            string `json:"level"`
	IsYellowVIPLevel string `json:"is_yellow_year_vip"`
}

QqRespUserInfo user info

type RespToken

type RespToken struct {
	Code         int    `json:"code"`
	Msg          string `json:"msg"`
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	OpenID       string `json:"openid"`
}

RespToken struct

type WbRespToken

type WbRespToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
	RemindIn    string `json:"remind_in"`
	UID         string `json:"uid"`
	IsRealName  string `json:"isRealName"`
	// contains filtered or unexported fields
}

WbRespToken response of me

type WbUserInfo

type WbUserInfo struct {
	ID              int    `json:"id"`
	ScreenName      string `json:"screen_name"`
	Name            string `json:"name"`
	Province        string `json:"province"`
	City            string `json:"city"`
	Location        string `json:"location"`
	Description     string `json:"description"`
	URL             string `json:"url"`
	ProfileImageURL string `json:"profile_image_url"`
	Domain          string `json:"domain"`
	Gender          string `json:"gender"`
	FriendsCount    int    `json:"friends_count"`
	FollowersCount  int    `json:"followers_count"`
	StatusesCount   int    `json:"statuses_count"`
	FavouritesCount int    `json:"favourites_count"`
	CreatedAt       string `json:"created_at"`
	Following       bool   `json:"following"`
	AllowAllActMsg  bool   `json:"allow_all_act_msg"`
	GeoEnabled      bool   `json:"geo_enabled"`
	Verified        bool   `json:"verified"`
	Status          struct {
		Annotations         []interface{} `json:"annotations"`
		CommentsCount       int           `json:"comments_count"`
		CreatedAt           string        `json:"created_at"`
		Favorited           bool          `json:"favorited"`
		Geo                 string        `json:"geo"`
		ID                  int           `json:"id"`
		InReplyToScreenName string        `json:"in_reply_to_screen_name"`
		InReplyToStatusID   string        `json:"in_reply_to_status_id"`
		InReplyToUserID     string        `json:"in_reply_to_user_id"`
		Mid                 string        `json:"mid"`
		RepostsCount        int           `json:"reposts_count"`
		Source              string        `json:"source"`
		Text                string        `json:"text"`
		Truncated           bool          `json:"truncated"`
	} `json:"status"`
	AllowAllComment  bool   `json:"allow_all_comment"`
	AvatarLarge      string `json:"avatar_large"`
	VerifiedReason   string `json:"verified_reason"`
	FollowMe         bool   `json:"follow_me"`
	OnlineStatus     int    `json:"online_status"`
	BiFollowersCount int    `json:"bi_followers_count"`
	// contains filtered or unexported fields
}

WbUserInfo user info

type Wechat

type Wechat struct {
	AppID       string
	AppSecret   string
	RedirectURL string
	HTTPRequest *utils.HTTPClient
}

Wechat struct @doc: https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

func (*Wechat) GetAuthorizeURL

func (w *Wechat) GetAuthorizeURL(args ...string) string

GetAuthorizeURL get authorize url

func (*Wechat) GetMe

func (w *Wechat) GetMe(accessToken string) (interface{}, error)

GetMe get me

func (*Wechat) GetUserInfo

func (w *Wechat) GetUserInfo(accessToken, openID string) (interface{}, error)

GetUserInfo get user info

func (*Wechat) RefreshToken

func (w *Wechat) RefreshToken(refreshToken string) (interface{}, error)

RefreshToken refresh token

func (*Wechat) Token

func (w *Wechat) Token(code string) (interface{}, error)

Token get token

type Weibo

type Weibo struct {
	ClientID     string
	ClientSecret string
	RedirectURL  string
	HTTPRequest  *utils.HTTPClient
}

Weibo struct

func (*Weibo) GetAuthorizeURL

func (w *Weibo) GetAuthorizeURL(args ...string) string

GetAuthorizeURL get authorize url @doc: https://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E @doc: https://open.weibo.com/wiki/Oauth2/authorize @explain: two document, ridiculous~

func (*Weibo) GetMe

func (w *Weibo) GetMe(accessToken string) (interface{}, error)

GetMe get me

func (*Weibo) GetUserInfo

func (w *Weibo) GetUserInfo(accessToken, openID string) (interface{}, error)

GetUserInfo get user info

func (*Weibo) RefreshToken

func (w *Weibo) RefreshToken(refreshToken string) (interface{}, error)

RefreshToken refresh token

func (*Weibo) Token

func (w *Weibo) Token(code string) (interface{}, error)

Token token

type WxRespToken

type WxRespToken struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	OpenID       string `json:"openid"`
	Scope        string `json:"scope"`
	UnionID      string `json:"unionid"`
	// contains filtered or unexported fields
}

WxRespToken response of me

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  interface{} `json:"privilege"`
	UnionID    string      `json:"unionid"`
	// contains filtered or unexported fields
}

WxUserInfo user info

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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