go - Twitter oauth golang getting Error "code":32,"message":"Could not authenticate you." -
i trying implement signing in twitter account in golang. first step in trying request token. reference used mrjones's code available @ below link. https://github.com/mrjones/oauth/blob/master/examples/twitterserver/twitterserver.go
i getting following error. please let me know going wrong:
{"errors":[{"code":32,"message":"could not authenticate you."}]}
var twitterconf = &twitterconfig{ clientid: " consumer key", clientsecret: "my consumer key secret", redirecturl: "http://localhost:8080/oauth/twitteroauth2callback", endpoint: serviceprovider{ requesttokenurl: "https://api.twitter.com/oauth/request_token", authorizetokenurl: "https://api.twitter.com/oauth/authorize", accesstokenurl: "https://api.twitter.com/oauth2/token", }, } func handletwitterlogin(res http.responsewriter, req *http.request, _ httprouter.params) { ctx := appengine.newcontext(req) params := url.values{} params.add(callback_param, twitterconf.redirecturl) params.add(consumer_key_param, twitterconf.clientid) params.add(nonce_param, strconv.formatint(rand.new(rand.newsource(time.now().unixnano())).int63(), 10)) params.add(signature_method_param, signature_method_hmac+"sha1") params.add(timestamp_param, strconv.formatint(time.now().unix(), 10)) params.add(version_param, oauth_version) params.add("oauth_token", "my oauth token") basestring := requeststring("post", twitterconf.endpoint.requesttokenurl, params) signature, err6 := sign(basestring, "my token secret") params.add(signature_param, signature) url, err := url.parse(twitterconf.endpoint.requesttokenurl) url.rawquery = params.encode() firsturl := url.string() reqnew, err2 := http.newrequest("post", firsturl, nil) if err2 != nil { log.errorf(ctx, "error in creating new request %+v ", err2) } reqnew.header.add("authorization", "oauth ") client := urlfetch.client(ctx) resp, err3 := client.do(reqnew) if err3 != nil { log.errorf(ctx, "error in doing client request %+v ", err3) } bodybytes, err4 := ioutil.readall(resp.body) resp.body.close() log.infof(ctx, "handletwitterlogin 5 ") if err4 != nil { log.errorf(ctx, "error in readall resp body %+v ", err4) } bodystr := string(bodybytes) //here getting above mentioned error }
these other functions used.
func requeststring(method string, url string, params url.values) string { result := method + "&" + escape(url) key, value := range params { if len(value) > 0 { result += escape("&") result += escape(fmt.sprintf("%s=%s", key, value)) } } return result } func sign(message string, tokensecret string) (string, error) { key := escape(twitterconf.clientsecret) + "&" + escape(tokensecret) h := hmac.new(crypto.sha1.new, []byte(key)) h.write([]byte(message)) rawsignature := h.sum(nil) base64signature := base64.stdencoding.encodetostring(rawsignature) return base64signature, nil }
able solve problem. made 2 changes:
encoded params variables before passing encryption create signature
basestring := "post"+"&"+ encode(twitterconf.endpoint.requesttokenurl) + "&"+encode(params.encode())
to create header, created own header string
headerstring := "oauth oauth_callback=\"http%3a%2f%2flocalhost%3a8080%2foauth%2ftwitteroauth2callback\",oauth_consumer_key=\"consumerkey\",oauth_nonce=\"" + params.get(nonce_param) + "\",oauth_signature=\"" + escape(signature) + "\",oauth_signature_method=\"hmac-sha1\",oauth_timestamp=\"" + params.get(timestamp_param) + "\",oauth_token=\"" + escape("oauth token") + "\",oauth_version=\"1.0\""
reqnew.header.add("authorization", headerstring)
this solved problem , started getting access token
Comments
Post a Comment