From c98343222db2d4c3fbff340d1fa8f99fd39fe9cc Mon Sep 17 00:00:00 2001 From: Suguivy Date: Tue, 29 Nov 2022 22:25:08 +0100 Subject: [PATCH] added code --- go.mod | 3 +++ main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..17198b1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module grames + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..53a3efe --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "net/url" + "os" +) + +var BASE_URL = "https://awoo.fai.st" +var BASE_API_URL = "https://awoo.fai.st/api/v1" + +func main() { + client_id, client_secret, err := get_app_credentials() + if err != nil { + log.Fatal(err) + } + fmt.Println(client_id, client_secret) +} + +func get_app_credentials() (string, string, error) { + contents, err := os.ReadFile("credentials.json") + if err != nil { + return create_app() + } + var json_data map[string]string + err = json.Unmarshal(contents, &json_data) + if err != nil { + return "", "", err + } + client_id := json_data["client_id"] + client_secret := json_data["client_secret"] + + return client_id, client_secret, nil +} + +func create_app() (string, string, error) { + data := url.Values{ + "client_name": {"grames"}, + "redirect_uris": {"urn:ietf:wg:oauth:2.0:oob"}, + } + + resp, err := http.PostForm(BASE_API_URL+"/apps", data) + + if err != nil { + return "", "", err + } + defer resp.Body.Close() + contents, err := io.ReadAll(resp.Body) + if err != nil { + return "", "", err + } + err = os.WriteFile("credentials.json", contents, 0644) + if err != nil { + return "", "", err + } + var json_data map[string]string + err = json.Unmarshal(contents, &json_data) + if err != nil { + return "", "", err + } + client_id := json_data["client_id"] + client_secret := json_data["client_secret"] + + return client_id, client_secret, nil +}