-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (80 loc) · 1.93 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package maidwhiteclient
import (
"context"
"fmt"
"golang.org/x/oauth2"
"io"
"net/http"
)
var Debug = false
type Client struct {
client *http.Client
accessToken string
oauth2Config oauth2.Config
}
type Token struct {
*oauth2.Token
}
const (
SCOPE_ALL = "*"
BASE_URL = "https://www.tih.tw/2"
)
//
func NewClient() *Client {
return &Client{
oauth2Config: oauth2.Config{
ClientID: "",
ClientSecret: "",
Endpoint: oauth2.Endpoint{
AuthURL: "https://www.tih.tw/o2/auth",
TokenURL: "https://www.tih.tw/o2/token",
},
RedirectURL: "",
Scopes: []string{SCOPE_ALL},
},
}
}
func init() {
oauth2.RegisterBrokenAuthHeaderProvider("https://www.tih.tw")
}
func (c *Client) SetClientID(clientID string) {
c.oauth2Config.ClientID = clientID
}
func (c *Client) SetClientSecret(clientSecret string) {
c.oauth2Config.ClientSecret = clientSecret
}
func (c *Client) SetRedirectURL(clientURL string) {
c.oauth2Config.RedirectURL = clientURL
}
func (c *Client) AuthCodeURL(state string) string {
return c.oauth2Config.AuthCodeURL(state)
// oauth2.Config
}
func (c *Client) Exchange(ctx context.Context, code string) (*Token, error) {
if c == nil {
e := fmt.Errorf("client is nil")
return nil, e
}
t, err := c.oauth2Config.Exchange(ctx, code)
if t != nil {
c.client = c.oauth2Config.Client(ctx, t)
}
return &Token{Token: t}, err
}
func (c *Client) SetAccessToken(accessToken string) {
ctx := context.Background()
c.accessToken = accessToken
t := &oauth2.Token{AccessToken: accessToken}
c.client = c.oauth2Config.Client(ctx, t)
}
func (c *Client) NewRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, BASE_URL+urlStr, body)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}
// req.Header.Add("Authorization", "Bearer "+c.client.Client.AccessToken)
return req, nil
}