Post photo to Twitter using Curl and Windows command line

限于喜欢 提交于 2021-01-29 12:57:37

问题


I am using Curl to post to the Twitter API via the Windows command line. I've already generated the oAuth headers and can post a tweet without a photo using:

curl -k -X POST "https://api.twitter.com/1.1/statuses/update.json" -d "status=Test" -H "Authorization: OAuth oauth_consumer_key=\"xxxxxxxxxxxxxxxxxxx\", oauth_nonce=\"xxxxxxxxxxxxxxxxxxxxxxx\", oauth_signature=\"xxxxxxxxxxxxxxxxxxx\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"xxxxxxxxxx\", oauth_token=\"xxxxxxxxxxxxxxxxx\", oauth_version=\"1.0\""

This works fine to post a status, but I can't seem to figure out how to post a photo with a status using the statuses/update_with_media endpoint. I know this endpoint is depreciated, but I want to use it for the time being.

I've tried some variations of:

curl  -k -X POST "https://api.twitter.com/1.1/statuses/update_with_media.json" -F \"[media]=@photo.jpg\" -F \"status=Test\" -H "Authorization: OAuth oauth_consumer_key=\"xxxxxxxxxx\", oauth_nonce=\"xxxxxxxxxxxxxxx\", oauth_signature=\"xxxxxxxxxxxxxxxx\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"xxxxxxx\", oauth_token=\"248911615-xxxxxxxxxxx\", oauth_version=\"1.0\""

with no luck. I always get a {"code":195,"message":"Missing or invalid url parameter."} error or {"code":32,"message":"Could not authenticate you."}.

I can't seem to figure out the right syntax and signature to post both a status and a photo.

I'd be happy to use the newer media/upload method if that's easier, but I haven't been able to get that working either.


回答1:


I figured out the syntax and signature to get the newer media/upload endpoint to work:

curl -k -X POST "https://upload.twitter.com/1.1/media/upload.json" -F "media=@file.jpg" -H "Authorization: OAuth oauth_consumer_key=\"xxxxxxxxxxxxxx\", oauth_nonce=\"xxxxxxxxxxxxx\", oauth_signature=\"xxxxxxxxxxx\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"xxxxxx\", oauth_token=\"xxxxxxxxxxx\", oauth_version=\"1.0\""

The key is not including the media file as part of the signature



来源:https://stackoverflow.com/questions/58161946/post-photo-to-twitter-using-curl-and-windows-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!