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 ( var (
AuthCookie string SnApiToken string
) )
func init() { func init() {
@ -16,17 +16,16 @@ func init() {
if err != nil { if err != nil {
log.Fatal("Error loading .env file") 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() flag.Parse()
if AuthCookie == "" { if SnApiToken == "" {
log.Fatal("auth cookie not set") log.Fatal("NEXT_AUTH_CSRF_TOKEN not set")
} }
} }
func main() { func main() {
stories := fetchTopStoriesFromHN() stories := fetchTopStoriesFromHN()
filtered := filterByRelevanceForSN(&stories) filtered := filterByRelevanceForSN(&stories)
log.Println(filtered)
for _, story := range *filtered { for _, story := range *filtered {
postToSN(&story) postToSN(&story)
} }

3
sn.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
) )
@ -47,7 +48,7 @@ func postToSN(story *Story) {
panic(err) panic(err)
} }
req.Header.Set("Content-Type", "application/json") 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 client := http.DefaultClient
resp, err := client.Do(req) resp, err := client.Do(req)