Exemplo n.º 1
0
func (c *Client) request(credentials *Credentials, url string, param web.ParamMap) (*Credentials, web.ParamMap, 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.ParamMap)
	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
}
Exemplo n.º 2
0
func (tw *twitterClient) twitterGet(url string, param web.ParamMap) (p []byte, err os.Error) {
	oauthClient.SignParam(tw.twitterToken, "GET", url, param)
	url = url + "?" + param.FormEncodedString()
	var resp *http.Response
	done := make(chan bool, 1)
	go func() {
		resp, _, err = http.Get(url)
		done <- true
	}()

	timeout := time.After(TWITTER_GET_TIMEOUT * 1e9) //
	select {
	case <-done:
		break
	case <-timeout:
		return nil, os.NewError("http Get timed out - " + url)
	}
	if resp == nil {
		panic("oops")
	}
	return readHttpResponse(resp, err)
}
Exemplo n.º 3
0
// Data in param must be URL escaped already.
func (tw *twitterClient) twitterPost(url string, param web.ParamMap) (p []byte, err os.Error) {
	oauthClient.SignParam(tw.twitterToken, "POST", url, param)

	// TODO: remove this dupe.
	var resp *http.Response
	done := make(chan bool, 1)
	go func() {
		resp, err = http.PostForm(url, param.StringMap())
		done <- true
	}()

	timeout := time.After(TWITTER_GET_TIMEOUT * 1e9) // post in this case.
	select {
	case <-done:
		break
	case <-timeout:
		return nil, os.NewError("http POST timed out - " + url)
	}
	if resp == nil {
		panic("oops")
	}
	return readHttpResponse(resp, err)
}
Exemplo n.º 4
0
// SignParam adds an OAuth signature to param.
func (c *Client) SignParam(credentials *Credentials, method, url string, param web.ParamMap) {
	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))
}