added code
This commit is contained in:
parent
2a1a88814a
commit
c98343222d
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue