modweb/examples/test/discord-login/get_token.go

40 lines
908 B
Go

package discord_login
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 getToken(code string) (*TokenResponse, error) {
res, err := http.PostForm(API_ENDPOINT + "/oauth2/token", url.Values{
"client_id": { "560595816289533954" },
"client_secret": { "g3Or-_xfcHIRK6M45tvTVnSU3dvo-04u" },
"grant_type": { "authorization_code" },
"code": { code },
"redirect_uri": { "http://localhost:8011/discord/callback" },
"scope": { "identify email" },
})
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
}