-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_handler.go
76 lines (61 loc) · 1.77 KB
/
twitter_handler.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
package handler
import (
"github.com/alexedwards/scs/v2"
"github.com/garyburd/go-oauth/oauth"
"github.com/laqiiz/airac/conn"
"html/template"
"net/http"
)
type TwitterCallbackRequest struct {
Token string
Verifier string
}
type twitterHandler struct {
session *scs.SessionManager
}
func NewTwitterHandler(session *scs.SessionManager) *twitterHandler {
return &twitterHandler{
session: session,
}
}
func (h *twitterHandler) AuthRedirect(w http.ResponseWriter, r *http.Request) {
con := conn.GetTwitterConnect()
rt, err := con.RequestTemporaryCredentials(nil, "http://localhost:8000/twitter/callback", nil)
if err != nil {
panic(err)
}
h.session.Put(r.Context(), "request_token", rt.Token)
h.session.Put(r.Context(), "request_token_secret", rt.Secret)
http.Redirect(w, r, con.AuthorizationURL(rt, nil), 302)
}
func (h *twitterHandler) Callback(w http.ResponseWriter, r *http.Request) {
request := TwitterCallbackRequest{
Token: r.URL.Query().Get("oauth_token"),
Verifier: r.URL.Query().Get("oauth_verifier"),
}
at, err := conn.GetAccessToken(
&oauth.Credentials{
Token: h.session.Get(r.Context(), "request_token").(string),
Secret: h.session.Get(r.Context(), "request_token_secret").(string),
},
request.Verifier,
)
if err != nil {
panic(err)
}
h.session.Put(r.Context(), "oauth_secret", at.Secret)
h.session.Put(r.Context(), "oauth_token", at.Token)
var a conn.Account
if err = conn.GetMe(at, &a); err != nil {
panic(err)
}
data := map[string]interface{}{}
data["ID"] = a.ID
data["ProfileImageURL"] = a.ProfileImageURL
data["ScreenName"] = a.ScreenName
data["Email"] = a.Email
tpl := template.Must(template.ParseFiles("view/twitter/callback.tpl"))
if err := tpl.Execute(w, data); err != nil {
_, _ = w.Write([]byte(err.Error()))
}
}