func (c *Client) request(credentials *Credentials, url string, param web.Values) (*Credentials, web.Values, os.Error) { c.SignParam(credentials, "POST", url, param) resp, err := http.PostForm(url, param.StringMap()) if err != nil { return nil, nil, err } p, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { return nil, nil, err } if resp.StatusCode != 200 { return nil, nil, os.NewError(fmt.Sprintf("OAuth server status %d, %s", resp.StatusCode, string(p))) } m := make(web.Values) err = m.ParseFormEncodedBytes(p) if err != nil { return nil, nil, err } credentials = &Credentials{Token: m.Get("oauth_token"), Secret: m.Get("oauth_token_secret")} if credentials.Token == "" { return nil, nil, os.NewError("No OAuth token in server result") } if credentials.Secret == "" { return nil, nil, os.NewError("No OAuth secret in server result") } return credentials, m, nil }
// getJSON fetches a URL and decodes the response body as JSON. func getJSON(urlStr string, param web.Values) (interface{}, os.Error) { if param != nil { urlStr = urlStr + "?" + param.FormEncodedString() } r, err := http.Get(urlStr) if err != nil { return nil, err } defer r.Body.Close() if r.StatusCode != 200 { return nil, os.NewError(fmt.Sprint("Status ", r.StatusCode)) } p, _ := ioutil.ReadAll(r.Body) var v interface{} err = json.NewDecoder(bytes.NewBuffer(p)).Decode(&v) if err != nil { return nil, err } return v, nil }
// getUrlEncodedForm fetches a URL and decodes the response body as a URL encoded form. func getUrlEncodedForm(urlStr string, param web.Values) (web.Values, os.Error) { if param != nil { urlStr = urlStr + "?" + param.FormEncodedString() } r, err := http.Get(urlStr) if err != nil { return nil, err } defer r.Body.Close() if r.StatusCode != 200 { return nil, os.NewError(fmt.Sprint("Status ", r.StatusCode)) } p, err := ioutil.ReadAll(r.Body) if err != nil { return nil, err } m := make(web.Values) err = m.ParseFormEncodedBytes(p) if err != nil { return nil, err } return m, nil }
// SignParam adds an OAuth signature to param. func (c *Client) SignParam(credentials *Credentials, method, url string, param web.Values) { param.Set("oauth_consumer_key", c.Credentials.Token) param.Set("oauth_signature_method", "HMAC-SHA1") param.Set("oauth_timestamp", strconv.Itoa64(time.Seconds())) param.Set("oauth_nonce", nonce()) param.Set("oauth_version", "1.0") if c.Scope != "" { param.Set("scope", c.Scope) } if credentials != nil { param.Set("oauth_token", credentials.Token) } param.Set("oauth_signature", signature(&c.Credentials, credentials, method, url, param)) }