简体   繁体   中英

Upload image to twitter using update_with_media - Lua

I am trying to post an image to twitter using update_with_media.json.

The following is a working code to update tweet with statuses/update.json

local url = "http://api.twitter.com/1/statuses/update.json"
local consumer_key = ""
local consumer_secret = ""
local token = ""
local token_secret = ""


local post_data = 
    {
    oauth_consumer_key = consumer_key,
    oauth_nonce        = get_nonce(),
    oauth_signature_method = "HMAC-SHA1",
    oauth_token        = token,
    oauth_timestamp    = get_timestamp(),
    oauth_version      = '1.0',
    oauth_token_secret = token_secret

    }

post_data["status"] = "Hello Twitter!"      
post_data = oAuthSign(url, "POST", post_data, consumer_secret)


r,c,h = http.request
   {
   url = url,
   method = "POST",
   headers = 
         {
         ["Content-Type"] = "application/x-www-form-urlencoded", 
         ["Content-Length"] = string.len(rawdata)
         },
   source = ltn12.source.string(post_data),
   sink = ltn12.sink.table(response)
   }

Now how do I modify the above code to upload images? The url would be "http://api.twitter.com/1/statuses/update_with_media.json" and headers["Content-Type"] would be "multipart/form-data"

But where and how do I specify the image to be uploaded?

Ok I got this working some time back.. This post by velluminteractive provides a good library to deal with twitter.

Admittedly the code is for a game engine called Corona SDK. But it shouldn't be too hard for others to use by removing Corona-specific elements from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM