How to reply to a tweet from the API


#1

Looking through the API I did not see a way to reply to a tweet from another user. Can anyone point me in the correct direction? I have the url to the Tweet from GET statuses/user_timeline and would like to reply to the tweet using an API.

Thanks


#2

You need to use the statuses/update endpoint and the in_reply_to_status_id parameter, passing in the ID of the Tweet you want to post a reply to.


#3

Thanks for the response so would the post look like this,

POST https://api.twitter.com/1.1/statuses/update.json?status=TextOFtheReplyTweet:in_reply_to_status_id=12345678990

where 123456789 is the ID of the tweet I want to reply to. Sorry for the dumb question but I have not used a post with two parameters as of yet. I am not sure what to separate the parameters with in the above I used a colon.

Thanks


#4

URL parameters are separated with &.

https://example.com/path?one=two&three=four

#5

Thanks for the info.

When I add the

in_reply_to_status_id=989215936967258112

parameter to the status update I get Response Text: {“errors”:[{“code”:32,“message”:“Could not authenticate you.”}]} error.

Could this be an enterprise feature that require additional authentication?


#6

This is not an enterprise feature, it is completely standard. You do need to have valid consumer key/token and access token/secret, and to calculate the OAuth signature correctly. Can you share a code snippet?


#7

This is a Excel VB application the actual sending of the command is done by this

Dim objRest As WinHttp.WinHttpRequest
Set objRest = New WinHttp.WinHttpRequest
objRest.Open "POST", "https://api.twitter.com/1.1/statuses/update.json", False
objRest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objRest.setRequestHeader "Authorization", strHeader
objRest.Send "status=" & "TextofTweet"

Which works fine and the tweet is sent, I know my key/token and access token/secret, are valid.
However If I change the objRest.Send command to
objRest.Send “status=” & “TextofTweet” & “&in_reply_to_status_id=989204711994781696”
it fails with the authorization error I mentioned, Response Text: {“errors”:[{“code”:32,“message”:“Could not authenticate you.”}]}.

the Authorization header above , strHeader is generated by the following,

'create basestring
tmpBase = “oauth_consumer_key=” & cnsOauthConsumerKey
tmpBase = tmpBase & “&” & “oauth_nonce=” & strNonce
tmpBase = tmpBase & “&” & “oauth_signature_method=” & cnsMethod
tmpBase = tmpBase & “&” & “oauth_timestamp=” & strTimestamp
tmpBase = tmpBase & “&” & “oauth_token=” & cnsOauthToken
tmpBase = tmpBase & “&” & “oauth_version=” & cnsOauthVersion
tmpBase = tmpBase & “&” & “status=” & strStatus
tmpBase = tmpBase & “&” & “in_reply_to_status_id=989204711994781696” (Again if I add this line it fails)

basestring = cnsAPIMethodP & “&” & UrlEncode((cnsURLPost)) & “&” & UrlEncode(tmpBase)

Then from the base string to generating the the Authorization header everything is the same. With the above snippets can you see anything wrong? The rest of the code is the same and works if I do not make the two changes above adding the “&in_reply_to_status_id=989204711994781696”
Thanks for the help,


#8

Are you able to send a tweet using twurl or any other standard library?
I would use twurl to look at the headers it is sending to twitter and make sure you’re doing the same.


#9

I have not attempted to use an alternate method to send the Tweet.
I presume my approach and understanding of the API as stated above is correct. So I guess it must be something in the code, and the next step would be to verify what is being sent. Thanks and I will keep you posted.


#10

It is not recommend to build your own OAuth encoding process. It’s very complex and is easy to get wrong. I suspect the error is because you are not sorting the parameters.

  1. Sort the list of parameters alphabetically by encoded key.

https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature


#11

Have you considered using your Excel FB file that outputs its unique contribution then maybe using twurl in a bash script to send the tweets? Anything to avoid reinventing the oAuth wheel.


#12

“abraham” nailed it – you are correct I had to Sort the parameters in alphabetically order by encoded key, just so happened I added the parameter in_reply_to_status_id which goes to the top of the list ahead of the oauth_* parameters. Sorted the list correctly and it works perfectly. I agree rolling your own code to do the OAuth encoding process is a head ace. I will look into the other suggestions offered by bobbe205 and andypiper. Thank you all for the help, I am very impressed with your knowledge and the support you all provided. Thanks again.