modweb/modules/auth/discord/get_me.go

45 lines
978 B
Go
Raw Normal View History

2020-12-05 02:35:40 +00:00
package discord
2020-12-05 02:24:54 +00:00
import (
"encoding/json"
"io/ioutil"
"net/http"
)
type MeResponse struct {
ID string `json:"id"`
Username string `json:"username"`
Avatar string `json:"avatar"`
Discriminator string `json:"discriminator"`
Email string `json:"email"`
Verified bool `json:"verified"`
Locale string `json:"locale"`
MfaEnabled bool `json:"mfa_enabled"`
Flags int `json:"flags"`
PremiumType int `json:"premium_type"`
}
func getMe(token *TokenResponse) (*MeResponse, error) {
req, err := http.NewRequest("GET", API_ENDPOINT+"/users/@me", nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", token.TokenType+" "+token.AccessToken)
client := http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var resp MeResponse
err = json.Unmarshal(body, &resp)
return &resp, nil
}