From 99d709217b92b20f68839cb5dcc4e6447b28d3f7 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 22 Apr 2020 10:59:33 -0400 Subject: [PATCH] example connection --- cmd/beebot/main.go | 52 +++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/cmd/beebot/main.go b/cmd/beebot/main.go index 9dffa0f..eee2bdc 100644 --- a/cmd/beebot/main.go +++ b/cmd/beebot/main.go @@ -4,18 +4,21 @@ import ( "flag" "fmt" "log" - "math/rand" - "net/http" - "strconv" "github.com/jzelinskie/geddit" ) const version = 1.0 +var userAgent = fmt.Sprintf("BeeBot:%.2f (by u/phlyingpenguin)", version) +var scopes = "identity read edit" + 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 userName = flag.String("user", "_BeeBot_", "Login name") +var password = flag.String("password", "nope", "Login password") +var reddit = flag.String("reddit", "MeadTest", "Default reddit") var o *geddit.OAuthSession @@ -28,40 +31,33 @@ func main() { o, err = geddit.NewOAuthSession( *clientID, *clientSecret, - "BeeBot", + userAgent, 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) + err = o.LoginAuth(*userName, *password) + if err != nil { + log.Fatal(2, err) + } - http.HandleFunc("/cb", mkCb(state)) - log.Fatal(http.ListenAndServe(*baseAddr, nil)) + me, err := o.AboutRedditor("_beebot_") + if err != nil { + log.Fatal(3, err) + } -} + log.Printf("%+v", me) -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) - } + cap, err := o.NewCaptcha() + if err != nil { + log.Fatal(4, err) + } - 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) + post := geddit.NewTextSubmission(*reddit, "Monthly challenge", "Hey this is a monthly challenge", false, cap) + sub, err := o.Submit(post) + if err != nil { + log.Fatal(5, err) } }