2020-04-11 17:51:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-04-11 18:18:57 +00:00
|
|
|
"fmt"
|
2020-04-11 17:51:10 +00:00
|
|
|
"log"
|
2020-04-11 18:18:57 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/jzelinskie/geddit"
|
2020-04-11 17:51:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const version = 1.0
|
|
|
|
|
2020-04-11 18:18:57 +00:00
|
|
|
var clientID = flag.String("id", "", "Client ID")
|
|
|
|
var clientSecret = flag.String("secret", "", "Client Secret")
|
|
|
|
var baseAddr = flag.String("url", "127.0.0.1:9595", "Base address")
|
|
|
|
|
|
|
|
var o *geddit.OAuthSession
|
|
|
|
|
2020-04-11 17:51:10 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
log.Printf("BeeBot v%.2f", version)
|
2020-04-11 18:18:57 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
o, err = geddit.NewOAuthSession(
|
|
|
|
*clientID,
|
|
|
|
*clientSecret,
|
|
|
|
"BeeBot",
|
|
|
|
fmt.Sprintf("http://%s/cb", *baseAddr),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(1, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := strconv.Itoa(rand.Intn(10000) * 1000)
|
|
|
|
url := o.AuthCodeURL(state, []string{"identity read edit"})
|
|
|
|
log.Printf("Visit %s to obtain auth code", url)
|
|
|
|
|
|
|
|
http.HandleFunc("/cb", mkCb(state))
|
|
|
|
log.Fatal(http.ListenAndServe(*baseAddr, nil))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkCb(startState string) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
state := r.URL.Query().Get("state")
|
|
|
|
code := r.URL.Query().Get("code")
|
|
|
|
if state != startState {
|
|
|
|
log.Fatalf("Unexpected state, %s != %s", startState, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := o.CodeAuth(code)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(2, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
me, err := o.AboutRedditor("_beebot_")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(3, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("%+v", me)
|
|
|
|
}
|
2020-04-11 17:51:10 +00:00
|
|
|
}
|