Compare commits
No commits in common. "4708c89c25ae9ec9c54eff2136fd6571e76677b4" and "88934bbbcc419e40c96b35c6e89debcea60286b5" have entirely different histories.
4708c89c25
...
88934bbbcc
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"linkheader"
|
"linkheader"
|
||||||
|
|
||||||
|
|
@ -25,12 +24,10 @@ type Endpoints struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type TokenResponse struct {
|
type TokenResponse struct {
|
||||||
Me string `json:"me"`
|
Me string `json:"me"`
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
TokenType string `json:"token_type"`
|
TokenType string `json:"token_type"`
|
||||||
Scope string `json:"scope"`
|
Scope string `json:"scope"`
|
||||||
Error string `json:"error"`
|
|
||||||
ErrorDescription string `json:"error_description"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetEndpoints(me *url.URL) (Endpoints, error) {
|
func GetEndpoints(me *url.URL) (Endpoints, error) {
|
||||||
|
|
@ -154,52 +151,17 @@ func Authorize(me *url.URL, endpoints Endpoints, clientID, scope string) (TokenR
|
||||||
reqValues.Add("client_id", clientID)
|
reqValues.Add("client_id", clientID)
|
||||||
reqValues.Add("me", me.String())
|
reqValues.Add("me", me.String())
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, endpoints.TokenEndpoint, strings.NewReader(reqValues.Encode()))
|
res, err := http.PostForm(endpoints.TokenEndpoint, reqValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tokenResponse, err
|
return tokenResponse, err
|
||||||
}
|
}
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
req.Header.Add("Accept", "application/json")
|
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return tokenResponse, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
dec := json.NewDecoder(res.Body)
|
||||||
return TokenResponse{}, fmt.Errorf("status code %d, instead of 200", res.StatusCode)
|
err = dec.Decode(&tokenResponse)
|
||||||
}
|
if err != nil {
|
||||||
|
return tokenResponse, err
|
||||||
if strings.HasPrefix(res.Header.Get("content-type"), "application/json") {
|
|
||||||
dec := json.NewDecoder(res.Body)
|
|
||||||
err = dec.Decode(&tokenResponse)
|
|
||||||
if err != nil {
|
|
||||||
return tokenResponse, fmt.Errorf("error while parsing response body with content-type %s as json: %s", res.Header.Get("content-type"), err)
|
|
||||||
}
|
|
||||||
if tokenResponse.Me == "" && tokenResponse.Error != "" {
|
|
||||||
return tokenResponse, fmt.Errorf("received error from endpoint: %s, %s", tokenResponse.Error, tokenResponse.ErrorDescription)
|
|
||||||
}
|
|
||||||
} else if strings.HasPrefix(res.Header.Get("content-type"), "application/x-www-form-urlencoded") {
|
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return tokenResponse, fmt.Errorf("error while reading response body with content-type %s: %s", res.Header.Get("content-type"), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
values, err := url.ParseQuery(string(body))
|
|
||||||
if err != nil {
|
|
||||||
return tokenResponse, fmt.Errorf("error while parsing response body with content-type %s as application/x-www-form-urlencoded: %s\nbody was: %q\n", res.Header.Get("content-type"), err, body)
|
|
||||||
}
|
|
||||||
|
|
||||||
if values.Get("me") == "" {
|
|
||||||
if errTxt := values.Get("error"); errTxt != "" {
|
|
||||||
return tokenResponse, fmt.Errorf("received error from endpoint: %s, %s", errTxt, values.Get("error_description"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tokenResponse.Me = values.Get("me")
|
|
||||||
tokenResponse.AccessToken = values.Get("token")
|
|
||||||
tokenResponse.TokenType = values.Get("token_type")
|
|
||||||
tokenResponse.Scope = values.Get("scope")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokenResponse, nil
|
return tokenResponse, nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user