modweb/modules/auth/discord/get_token.go

39 lines
859 B
Go

package discord
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
func (m *Module) getToken(code string) (*TokenResponse, error) {
res, err := http.PostForm(API_ENDPOINT+"/oauth2/token", url.Values{
"client_id": {m.ClientID},
"client_secret": {m.ClientSecret},
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {m.RedirectURI},
"scope": {m.Scope},
})
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var resp TokenResponse
err = json.Unmarshal(body, &resp)
return &resp, nil
}