Files
novatask/internal/pkg/tribally/tribally.go
2025-03-20 17:15:54 +08:00

49 lines
1.0 KiB
Go

package tribally
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func BindTribally(apiKey, userId string) (string, error) {
url := "https://api.tribally.games/authenticate"
payload := strings.NewReader(fmt.Sprintf(`{"playerId": "%s"}`, userId))
req, err := http.NewRequest(http.MethodPost, url, payload)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("x-api-key", apiKey)
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
result := struct {
Error string `json:"error"`
Code string `json:"code"`
Data struct {
AuthURL string `json:"authUrl"`
} `json:"data"`
}{}
err = json.Unmarshal(body, &result)
if err != nil {
return "", err
}
if result.Error != "" {
return "", fmt.Errorf("error: %s, code: %s", result.Error, result.Code)
}
return result.Data.AuthURL, nil
}