Codebase list golang-github-nlopes-slack / 71fba5c
Support full OAauth response (for adding hooks and bot users via OAuth) John Boiles 8 years ago
1 changed file(s) with 30 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
44 "net/url"
55 )
66
7 type oAuthResponseFull struct {
8 AccessToken string `json:"access_token"`
9 Scope string `json:"scope"`
7 type OAuthResponseIncomingWebhook struct {
8 URL string `json:"url"`
9 Channel string `json:"channel"`
10 ConfigurationURL string `json:"configuration_url"`
11 }
12
13 type OAuthResponseBot struct {
14 BotUserID string `json:"bot_user_id"`
15 BotAccessToken string `json:"bot_access_token"`
16 }
17
18 type OAuthResponse struct {
19 AccessToken string `json:"access_token"`
20 Scope string `json:"scope"`
21 TeamName string `json:"team_name"`
22 TeamID string `json:"team_id"`
23 IncomingWebhook OAuthResponseIncomingWebhook `json:"incoming_webhook"`
24 Bot OAuthResponseBot `json:"bot"`
1025 SlackResponse
1126 }
1227
1328 // GetOAuthToken retrieves an AccessToken
1429 func GetOAuthToken(clientID, clientSecret, code, redirectURI string, debug bool) (accessToken string, scope string, err error) {
30 response, err := GetOAuthResponse(clientID, clientSecret, code, redirectURI, debug)
31 if err != nil {
32 return "", "", err
33 }
34 return response.AccessToken, response.Scope, nil
35 }
36
37 func GetOAuthResponse(clientID, clientSecret, code, redirectURI string, debug bool) (resp *OAuthResponse, err error) {
1538 values := url.Values{
1639 "client_id": {clientID},
1740 "client_secret": {clientSecret},
1841 "code": {code},
1942 "redirect_uri": {redirectURI},
2043 }
21 response := &oAuthResponseFull{}
44 response := &OAuthResponse{}
2245 err = post("oauth.access", values, response, debug)
2346 if err != nil {
24 return "", "", err
47 return nil, err
2548 }
2649 if !response.Ok {
27 return "", "", errors.New(response.Error)
50 return nil, errors.New(response.Error)
2851 }
29 return response.AccessToken, response.Scope, nil
52 return response, nil
3053 }