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"
"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,31 +31,14 @@ 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)
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)
err = o.LoginAuth(*userName, *password)
if err != nil {
log.Fatal(2, err)
}
@ -63,5 +49,15 @@ func mkCb(startState string) http.HandlerFunc {
}
log.Printf("%+v", me)
cap, err := o.NewCaptcha()
if err != nil {
log.Fatal(4, err)
}
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)
}
}