Twitter OAuth error 32 (401) using Unirest Java -
i using unirest build http request:
httprequestwithbody request = unirest .post("https://api.twitter.com/1.1/statuses/update.json") .header("authorization", oauth) .header("content-type", "application/x-www-form-urlencodedheader") .querystring("status", "123testtest");
so far can't see issues this. maybe problem in creating oauth signature?
private string percentencode(long toencode) { try { string returnstring = urlencoder.encode(toencode.tostring(), "utf-8"); return returnstring.replace("+", "%20"); } catch (unsupportedencodingexception e) { return ""; } } private string percentencode(string toencode) { try { string returnstring = urlencoder.encode(toencode, "utf-8"); return returnstring.replace("+", "%20"); } catch (unsupportedencodingexception e) { return ""; } } private string generatenonce() { string nonce = ""; string possible = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; (int = 0; < 45; i++) { nonce += possible.charat((int) math.floor(math.random() * possible.length())); } return nonce; } public string buildheader() throws unsupportedencodingexception { string parameter_string = ""; string nonce = generatenonce(); parameter_string += "include_entities=true"; parameter_string += "&oauth_consumer_key=" + percentencode(consumerkey); parameter_string += "&oauth_nonce=" + percentencode(nonce); parameter_string += "&oauth_signature_method=hmac-sha1"; parameter_string += "&oauth_status=" + percentencode("123testtest"); parameter_string += "&oauth_timestamp=" + percentencode(instant.now().getepochsecond()); parameter_string += "&oauth_token=" + percentencode(accessstoken); parameter_string += "&oauth_version=1.0"; string signature_base_string = ""; signature_base_string += percentencode("post"); signature_base_string += '&' + percentencode(postapiurl); signature_base_string += '&' + percentencode(parameter_string); string signing_key = percentencode(this.consumersecret) + '&' + percentencode(this.accesstokensecret); byte[] hmac_signature_bytes = hmacutils.hmacsha1(signing_key, signature_base_string); string hmac_signature = new string(base64.getencoder().encode(hmac_signature_bytes)); string oauthstring = "oauth "; oauthstring += "oauth_consumer_key=\"" + percentencode(consumerkey) + '"'; oauthstring += ", oauth_nonce=\"" + percentencode(nonce) + '"'; oauthstring += ", oauth_signature=\"" + percentencode(hmac_signature) + '"'; oauthstring += ", oauth_signature_method=\"hmac-sha1\""; oauthstring += ", oauth_timestamp=\"" + percentencode(instant.now().getepochsecond()) + '"'; oauthstring += ", oauth_token=\"" + percentencode(accessstoken) + '"'; oauthstring += ", oauth_version=\"1.0\""; return oauthstring; }
i'd thankful advice, lost. authorization header looks in http request:
oauth oauth_consumer_key="xxx", oauth_nonce="xxx", oauth_signature="xxx", oauth_signature_method="hmac-sha1", oauth_timestamp="1478367183", oauth_token="xxx", oauth_version="1.0"
this response server:
[18:33:03]: {"errors":[{"code":32,"message":"could not authenticate you."}]} [18:33:03]: 401
Comments
Post a Comment