added code

This commit is contained in:
Suguivy 2022-11-29 22:25:08 +01:00
parent 2a1a88814a
commit c98343222d
2 changed files with 72 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module grames
go 1.19

69
main.go Normal file
View File

@ -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
}