func (c *oauthStreamClient) connect() (*http.Response, os.Error) { c.httpClient = httplib.Post(c.url) for k, v := range c.headers { c.httpClient.Header(k, v) } var body bytes.Buffer for k, v := range c.params { body.WriteString(URLEscape(k)) body.WriteString("=") body.WriteString(URLEscape(v)) } c.httpClient.Body(body.String()) //make the new connection resp, err := c.httpClient.AsResponse() if err != nil { return resp, err } if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) { return nil, os.NewError(resp.Status) } return resp, nil }
func (o *OAuthClient) GetAccessToken(requestToken *RequestToken, OAuthVerifier string) (*AccessToken, os.Error) { if requestToken == nil || requestToken.OAuthToken == "" || requestToken.OAuthTokenSecret == "" { return nil, os.NewError("Invalid Request token") } nonce := getNonce(40) params := map[string]string{ "oauth_nonce": nonce, "oauth_token": requestToken.OAuthToken, "oauth_verifier": OAuthVerifier, "oauth_signature_method": "HMAC-SHA1", "oauth_timestamp": strconv.Itoa64(time.Seconds()), "oauth_consumer_key": o.ConsumerKey, "oauth_version": "1.0", } base := signatureBase("POST", requestTokenUrl.Raw, params) signature := signRequest(base, o.ConsumerSecret, requestToken.OAuthTokenSecret) params["oauth_signature"] = URLEscape(signature) authBuf := bytes.NewBufferString("OAuth ") i := 0 for k, v := range params { authBuf.WriteString(fmt.Sprintf("%s=%q", k, v)) if i < len(params)-1 { authBuf.WriteString(", ") } i++ } request := httplib.Post(accessTokenUrl.Raw) request.Header("Authorization", authBuf.String()) request.Body("") resp, err := request.AsString() tokens, err := http.ParseQuery(resp) if err != nil { return nil, err } at := AccessToken{ OAuthTokenSecret: tokens["oauth_token_secret"][0], OAuthToken: tokens["oauth_token"][0], UserId: tokens["user_id"][0], ScreenName: tokens["screen_name"][0], } return &at, nil }
func (o *OAuthClient) GetRequestToken(callback string) *RequestToken { nonce := getNonce(40) params := map[string]string{ "oauth_nonce": nonce, "oauth_callback": URLEscape(callback), "oauth_signature_method": "HMAC-SHA1", "oauth_timestamp": strconv.Itoa64(time.Seconds()), "oauth_consumer_key": o.ConsumerKey, "oauth_version": "1.0", } base := signatureBase("POST", requestTokenUrl.Raw, params) signature := signRequest(base, o.ConsumerSecret, "") params["oauth_signature"] = URLEscape(signature) authBuf := bytes.NewBufferString("OAuth ") i := 0 for k, v := range params { authBuf.WriteString(fmt.Sprintf("%s=%q", k, v)) if i < len(params)-1 { authBuf.WriteString(", ") } i++ } request := httplib.Post(requestTokenUrl.Raw) request.Header("Authorization", authBuf.String()) request.Body("") resp, err := request.AsString() tokens, err := http.ParseQuery(resp) if err != nil { println(err.String()) } confirmed, _ := strconv.Atob(tokens["oauth_callback_confirmed"][0]) rt := RequestToken{ OAuthTokenSecret: tokens["oauth_token_secret"][0], OAuthToken: tokens["oauth_token"][0], OAuthCallbackConfirmed: confirmed, } return &rt }