Refactor SN API authorization
This commit is contained in:
		
							parent
							
								
									aad977b875
								
							
						
					
					
						commit
						792eb8f40d
					
				| @ -1 +1 @@ | ||||
| AUTH_COOKIE= | ||||
| NEXT_AUTH_CSRF_TOKEN= | ||||
							
								
								
									
										9
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								main.go
									
									
									
									
									
								
							| @ -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
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								sn.go
									
									
									
									
									
								
							| @ -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) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user