You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB

package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"os"
)
type Token struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
}
type TokenRequest struct {
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
func getToken(conf *Config) (Token, error) {
var tok Token
client := http.Client{}
var b bytes.Buffer
var tokReq TokenRequest
tokReq.ClientID = conf.ClientID
tokReq.ClientSecret = conf.ClientSecret
tokReq.GrantType = "client_credentials"
err := json.NewEncoder(&b).Encode(&tokReq)
if err != nil {
return tok, err
}
req, err := http.NewRequest("POST", "https://api.atlassian.com/oauth/token", &b)
if err != nil {
return tok, err
}
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return tok, err
}
defer res.Body.Close()
err = json.NewDecoder(io.TeeReader(res.Body, os.Stdout)).Decode(&tok)
if err != nil {
return tok, err
}
log.Println(tok)
return tok, nil
}