grames/main.go

70 lines
1.4 KiB
Go

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
}