func PrepareOAuthHeaders(reqMethod string, endUrl string, params url.Values) string { params.Set("oauth_signature", createSignature(reqMethod, endUrl, encodedOauthParams(params))) sortedKeys := getSortedKeys(params) header := "OAuth " for key := range sortedKeys { header += sortedKeys[key] + "=\"" header += utils.Encode(params.Get(sortedKeys[key]), false) + "\", " } return header[:len(header)-len(", ")] }
func postTweet(status string) { fmt.Println("Post Added in Twitter Stream " + twitter.AccessToken) params := twitter.GetHeadersMap() params.Set("status", status) method := "POST" endUrl := "/1.1/statuses/update.json" header := twitter.PrepareOAuthHeaders(method, endUrl, params) response := utils.ProcessRequest(method, header, twitter.ApiUrl+endUrl+"?status="+utils.Encode(status, false), nil) fmt.Println(string(response)) }
func encodedOauthParams(params url.Values) string { sortedKeys := getSortedKeys(params) encodedString := "" for key := range sortedKeys { encodedString += sortedKeys[key] + "=" encodedString += utils.Encode(params.Get(sortedKeys[key]), false) + "&" } fmt.Println(params.Encode()) fmt.Println(encodedString[:len(encodedString)-len("&")]) return encodedString[:len(encodedString)-len("&")] }
func createSignature(reqMethod string, endUrl string, paramsEncode string) (sig string) { sign_key := []byte(utils.Encode(client_secret, false) + "&" + utils.Encode(AccessTokenSecret, false)) h := hmac.New(sha1.New, sign_key) //TODO: Little Suspicious h.Write([]byte(reqMethod + "&" + utils.Encode(ApiUrl+endUrl, false) + "&" + utils.Encode(paramsEncode, false))) return base64.StdEncoding.EncodeToString(h.Sum(sign_key[:0])) }
func SignIn(w http.ResponseWriter, req *http.Request) { fmt.Println("Trying to Auth Twitter") // Preparing Params params := GetHeadersMap() params.Set("oauth_callback", redirect_uri) method := "POST" endUrl := "/oauth/request_token" header := PrepareOAuthHeaders(method, endUrl, params) oauth_token_response := string(utils.ProcessRequest(method, header, ApiUrl+endUrl, nil)) fmt.Println(oauth_token_response) if strings.Contains(oauth_token_response, "oauth_token") { AccessToken = strings.Split(strings.Split(oauth_token_response, "&")[0], "=")[1] fmt.Println(string(utils.ProcessRequest("GET", "", ApiUrl+"/oauth/authenticate?oauth_token="+utils.Encode(AccessToken, false), nil))) // TODO: Replace this with LOGIN Page } else { fmt.Println(oauth_token_response) } }