Refactor SN API authorization

This commit is contained in:
ekzyis 2023-04-16 17:28:38 +02:00
parent aad977b875
commit 792eb8f40d
3 changed files with 7 additions and 7 deletions

View File

@ -1 +1 @@
AUTH_COOKIE=
NEXT_AUTH_CSRF_TOKEN=

View File

@ -8,7 +8,7 @@ import (
)
var (
AuthCookie string
SnApiToken string
)
func init() {
@ -16,17 +16,16 @@ func init() {
if err != nil {
log.Fatal("Error loading .env file")
}
flag.StringVar(&AuthCookie, "auth_cookie", "", "Cookie required for authorization")
flag.StringVar(&SnApiToken, "NEXT_AUTH_CSRF_TOKEN", "", "Token required for authorizing requests to stacker.news/api/graphql")
flag.Parse()
if AuthCookie == "" {
log.Fatal("auth cookie not set")
if SnApiToken == "" {
log.Fatal("NEXT_AUTH_CSRF_TOKEN not set")
}
}
func main() {
stories := fetchTopStoriesFromHN()
filtered := filterByRelevanceForSN(&stories)
log.Println(filtered)
for _, story := range *filtered {
postToSN(&story)
}

3
sn.go
View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
@ -47,7 +48,7 @@ func postToSN(story *Story) {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", AuthCookie)
req.Header.Set("Cookie", fmt.Sprintf("__Host-next-auth.csrf-token=%s", SnApiToken))
client := http.DefaultClient
resp, err := client.Do(req)