func (r *Request) request(method string, uri string, data url.Values) ([]byte, error) { url := fmt.Sprintf("https://www.pivotaltracker.com/services/v5/%s", uri) fmt.Println("URL:", url) return utils.Request(method, url, data, map[string]string{"X-TrackerToken": r.Token}) }
func (mvn *Mavenlink) request(method string, url string, data url.Values) ([]byte, error) { auth := fmt.Sprintf("Bearer %s", mvn.Token) headers := map[string]string{ "Content-Type": "application/x-www-form-urlencoded", "Authorization": auth, } return utils.Request(method, url, data, headers) }
func callbackHandler(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() domain := params.Get("domain") user := params.Get("user") chanId := params.Get("channel") fmt.Println("callbackHandler", params) if params.Get("error") != "" { mvnError(domain, chanId, user, params.Get("error_description")) return } // client_id is the ID assigned to your application by Mavenlink // client_secret is the secret token assigned to your application by Mavenlink // grant_type must be set to "authorization_code" in order to exchange a code for an access token // code is the value that was returned in the code query parameter when Mavenlink redirected back to your redirect_uri // redirect_uri is the exact same value that you used in the original request to /oauth/authorize // 199c12d7ebc29800ec202fbad0b2585a1f84950536851527a95a024881adbb2b code := params.Get("code") callback := os.Getenv("MAVENLINK_CALLBACK") callback = fmt.Sprintf("%s?domain=%s&user=%s&channel=%s", callback, domain, user, chanId) rp := url.Values{} rp.Set("client_id", os.Getenv("MAVENLINK_APP_ID")) rp.Set("client_secret", os.Getenv("MAVENLINK_SECRET")) rp.Set("grant_type", "authorization_code") rp.Set("code", code) rp.Set("redirect_uri", callback) fmt.Println("Sending", rp) resp, err := utils.Request( "POST", "https://app.mavenlink.com/oauth/token", rp, map[string]string{}) if err != nil { mvnError(domain, chanId, user, err.Error()) return } fmt.Println("Response from Mavenlink:", string(resp)) var b *MvnAuthResponse err = json.Unmarshal(resp, &b) if err != nil { fmt.Println("Error unmarshal", err.Error()) mvnError(domain, chanId, user, err.Error()) return } if b.Error != "" { mvnError(domain, chanId, user, b.ErrorDescription) return } err = db.SetSetting(user, "MAVENLINK_TOKEN", b.AccessToken) if err != nil { mvnError(domain, chanId, user, err.Error()) return } msg := fmt.Sprintf("Saved Mavenlink authentication token for @%s", user) MvnSend(domain, chanId, msg) w.WriteHeader(http.StatusOK) }