Support full OAauth response (for adding hooks and bot users via OAuth)
John Boiles
8 years ago
4 | 4 | "net/url" |
5 | 5 | ) |
6 | 6 | |
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"` | |
10 | 25 | SlackResponse |
11 | 26 | } |
12 | 27 | |
13 | 28 | // GetOAuthToken retrieves an AccessToken |
14 | 29 | 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) { | |
15 | 38 | values := url.Values{ |
16 | 39 | "client_id": {clientID}, |
17 | 40 | "client_secret": {clientSecret}, |
18 | 41 | "code": {code}, |
19 | 42 | "redirect_uri": {redirectURI}, |
20 | 43 | } |
21 | response := &oAuthResponseFull{} | |
44 | response := &OAuthResponse{} | |
22 | 45 | err = post("oauth.access", values, response, debug) |
23 | 46 | if err != nil { |
24 | return "", "", err | |
47 | return nil, err | |
25 | 48 | } |
26 | 49 | if !response.Ok { |
27 | return "", "", errors.New(response.Error) | |
50 | return nil, errors.New(response.Error) | |
28 | 51 | } |
29 | return response.AccessToken, response.Scope, nil | |
52 | return response, nil | |
30 | 53 | } |