modweb/modules/auth/discord/get_token.go

40 lines
864 B
Go
Raw Normal View History

2020-12-05 02:24:54 +00:00
package discord_auth
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
}