-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
72 lines (64 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"encoding/json"
"fmt"
"github.com/beego/beemod"
client2 "github.com/beego/beemod/pkg/oauth/client"
"github.com/mitchellh/mapstructure"
"net/http"
)
var config = `
[beego.oauth2.dev]
state = "beego"
appID = "sfdhjaksfddsajks"
appSecret = "mnjUYj8rlXXKGS2RNgsdad7lygWrjJzjD5"
authURL = "http://oauthadmin.yitum.com/user/login"
tokenURL = "http://oauthadmin.yitum.com/api/v1/oauth/token"
redirectURI = "http://localhost:8000/api/code"
userInfoURL = "http://oauthadmin.yitum.com/api/v1/oauth/user"
Scopes=[]
`
type UserInfo struct {
Uid int ` json:"uid" ` // uid
Nickname string `json:"nickname" ` // nickname
Email string ` json:"email" ` // email
Avatar string ` json:"avatar"` // avatar
Password string ` json:"password" ` // password
Status int64 ` json:"status" ` // status
Gender int64 ` json:"gender"` // gender
Birthday int64 ` json:"birthday" ` // birthday
LastLoginIp string ` json:"lastLoginIp" ` // last_login_ip
}
type RespUser struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data UserInfo `json:"data"`
}
func main() {
err := beemod.Register(
client2.DefaultBuild,
).SetCfg([]byte(config), "toml").Run()
if err != nil {
panic("register err:" + err.Error())
}
client := client2.Invoker("dev")
http.HandleFunc("/api/login", func(writer http.ResponseWriter, request *http.Request) {
page := client.LoginPage()
http.Redirect(writer, request, page, http.StatusFound)
})
http.HandleFunc("/api/code", func(writer http.ResponseWriter, request *http.Request) {
query := request.URL.Query()
code := query["code"][0]
state := query["state"][0]
token, _ := client.GetAccessToken(state, code)
var user RespUser
rsp, _ := client.GetUserInfo(token, user)
if err := mapstructure.Decode(rsp, &user); err != nil {
fmt.Println(err)
}
writer.Header().Set("content-type", "text/json")
msg, _ := json.Marshal(user.Data)
_, _ = writer.Write(msg)
})
_ = http.ListenAndServe(":8000", nil)
}