/
main.go
76 lines (66 loc) · 1.73 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
package main
import (
"fmt"
"github.com/ChimeraCoder/anaconda"
"github.com/garyburd/go-oauth/oauth"
"golang.org/x/crypto/ssh/terminal"
"html/template"
"log"
"net/http"
"os"
"path"
)
var rootDir string
var consumerKey, consumerSecret string
var credential *oauth.Credentials
func IndexHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(path.Join(rootDir, "index.html"))
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
t.Execute(w, nil)
}
func RequestTokenHandler(w http.ResponseWriter, r *http.Request) {
anaconda.SetConsumerKey(consumerKey)
anaconda.SetConsumerSecret(consumerSecret)
url, tmpCred, err := anaconda.AuthorizationURL("http://localhost:8000/access_token")
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
credential = tmpCred
http.Redirect(w, r, url, http.StatusFound)
}
func AccessTokenHandler(w http.ResponseWriter, r *http.Request) {
c, _, err := anaconda.GetCredentials(credential, r.URL.Query().Get("oauth_verifier"))
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
t, err := template.ParseFiles(path.Join(rootDir, "access_token.html"))
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
t.Execute(w, c)
}
func main() {
rootDir = path.Join(os.Getenv("GOPATH"), "src/github.com/utahta/go-twitter-oauth")
fmt.Println("Enter ConsumerKey: ")
ck, err := terminal.ReadPassword(0)
if err != nil {
log.Fatal(err)
}
consumerKey = string(ck)
fmt.Println("Enter ConsumerSecret: ")
cs, err := terminal.ReadPassword(0)
if err != nil {
log.Fatal(err)
}
consumerSecret = string(cs)
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/request_token", RequestTokenHandler)
http.HandleFunc("/access_token", AccessTokenHandler)
http.ListenAndServe(":8000", nil)
}