example connection

This commit is contained in:
Chris Sexton 2020-04-22 10:59:33 -04:00
parent d11b2382ef
commit 99d709217b
1 changed files with 24 additions and 28 deletions

View File

@ -4,18 +4,21 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"math/rand"
"net/http"
"strconv"
"github.com/jzelinskie/geddit" "github.com/jzelinskie/geddit"
) )
const version = 1.0 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 clientID = flag.String("id", "", "Client ID")
var clientSecret = flag.String("secret", "", "Client Secret") var clientSecret = flag.String("secret", "", "Client Secret")
var baseAddr = flag.String("url", "127.0.0.1:9595", "Base address") 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 var o *geddit.OAuthSession
@ -28,40 +31,33 @@ func main() {
o, err = geddit.NewOAuthSession( o, err = geddit.NewOAuthSession(
*clientID, *clientID,
*clientSecret, *clientSecret,
"BeeBot", userAgent,
fmt.Sprintf("http://%s/cb", *baseAddr), fmt.Sprintf("http://%s/cb", *baseAddr),
) )
if err != nil { if err != nil {
log.Fatal(1, err) log.Fatal(1, err)
} }
state := strconv.Itoa(rand.Intn(10000) * 1000) err = o.LoginAuth(*userName, *password)
url := o.AuthCodeURL(state, []string{"identity read edit"}) if err != nil {
log.Printf("Visit %s to obtain auth code", url) log.Fatal(2, err)
}
http.HandleFunc("/cb", mkCb(state)) me, err := o.AboutRedditor("_beebot_")
log.Fatal(http.ListenAndServe(*baseAddr, nil)) if err != nil {
log.Fatal(3, err)
}
} log.Printf("%+v", me)
func mkCb(startState string) http.HandlerFunc { cap, err := o.NewCaptcha()
return func(w http.ResponseWriter, r *http.Request) { if err != nil {
state := r.URL.Query().Get("state") log.Fatal(4, err)
code := r.URL.Query().Get("code") }
if state != startState {
log.Fatalf("Unexpected state, %s != %s", startState, state)
}
err := o.CodeAuth(code) post := geddit.NewTextSubmission(*reddit, "Monthly challenge", "Hey this is a monthly challenge", false, cap)
if err != nil { sub, err := o.Submit(post)
log.Fatal(2, err) if err != nil {
} log.Fatal(5, err)
me, err := o.AboutRedditor("_beebot_")
if err != nil {
log.Fatal(3, err)
}
log.Printf("%+v", me)
} }
} }