Posting images within a message post or tweet to Facebook and Twitter on Android

I’ve used the social plugin on iOS, and that works great for posting an image directly from my app to both sites.

However, it seems that plugin only works on iOS, and even then it only works if the user has signed into those services in iOS’s settings app.

So is there a way to easily post images directly to FB and Twitter from within the app, without having to upload the image somewhere else first?

I’ve seen that there is an update_with_media method for Twitter, but I don’t see any sign of it in the Twitter sample code:

https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media

It says “Up to max_media_per_upload files may be specified in the request, each named media[]. Supported image formats are PNG, JPG and GIF. Animated GIFs are not supported. This data must be either the raw image bytes or encoded as base64.” How would we encode our images like this in Corona?

As for Facebook, all code I see implies that the image has to be hosted online, and you provide a url. But seeing as the native plugin can post the image directly, is it possible for us to do this in a dialog post?

Edit: I should add, I want the image to be included as part of a feed post, not just a regular image upload.

I’ve got this code which posts either photos or videos to fb albums (after the app has used the sdk to login to fb of course)

[lua]

           if( isPhotoPost == true) then
                if( (utils.isEmpty(imageFilenameShare) ~= true) and (this.sharePopup.MessageButton.text.text == “On”)) then             – They want the composite image, right?
                    fileLocation = imageFilenameShare       – Special image with text composited into it…         
                else                                                        
                    fileLocation = utils.getBaseFilename(imageFilenameLarge)
                end
            
            – New method of posting it up… As a FB photo… (Not as a stream post)
            
                local attachment = {
                        message = currMess.MESSAGE,
                        source = {
                                baseDir=system.TemporaryDirectory,
                                filename=fileLocation,
                                type=“image”
                        },
                    }
                facebook.request( “me/photos”, “POST”, attachment )    
            else
                – else video— looks simple, but there was a lot of monkey business discovering undocumented corona technique to post video to fb…
                fileLocation = utils.getBaseFilename(this.videoName)
                    
                local uploadParams = {}
                uploadParams[fileLocation] = {            
                    baseDir=system.TemporaryDirectory,
                    filename=fileLocation,
                    type=“video/mp4”               
                }
                
                uploadParams.title=tostring(mojoGroups.currentMenu.PTITLE)
                uploadParams.description=tostring(currMess.MESSAGE)
                
                print(" – sending fb request")
                facebook.request( “me/videos”, “POST”, uploadParams )  

[/lua]

I can’t recall if I tried just putting it in the feed first (my comment sort of indicates I changed later to posting into the albums). But in this case, there’s no base64 stuff or hosting somewhere else, etc. Also, I believe fb returns a photo id reference/number when photos are posted into the users album this way. Maybe the returned facebook object ID  (returned from the facebook.request() call above) could be used as the online reference for a feed post, as you mentioned.

Thank you mpappas, I want to post straight to the feed rather than to an album, but I’ll take a look later and see if I can work this to fit my needs.

I found a site that had an example of posting images to twitter supposedly working: 

http://velluminteractive.com/upload-images-to-twitter-from-corona-sdk/

but it would seem it’s outdated and no longer works. For a start it’s still using the “upload.twitter.com/1/” url rather than “api.twitter.com/1.1/”.

Does anyone at Corona have any experience with posting images to Twitter, I suspect it’s probably quite simple for someone more experienced?

update_with_media has been part of the Twitter api for a few years now as far as I can tell, so I’m surprised it’s not in the sample code already tbh.

Here’s the response data I receive:

responseHeaders : Content-Type application/json;charset=utf-8 Date Thu, 27 Feb 2014 19:55:36 GMT x-xss-protection 1; mode=block x-mediaratelimit-class photos Expires Tue, 31 Mar 1981 05:00:00 GMT Last-Modified Thu, 27 Feb 2014 19:55:36 GMT status 403 Forbidden Pragma no-cache Server tfe x-content-type-options nosniff Content-Length 83 strict-transport-security max-age=631138519 x-mediaratelimit-reset 1393616856 Content-Encoding gzip x-transaction dc72366e2ce6319e Cache-Control no-cache, no-store, must-revalidate, pre-check=0, post-check=0 x-access-level read-write x-frame-options SAMEORIGIN x-mediaratelimit-limit 1000 x-mediaratelimit-remaining 996 responseType text phase ended bytesEstimated -1 rest of data: response {"errors":[{"code":189,"message":"Error creating status."}]} name networkRequest bytesTransferred 60 status 403 url https://api.twitter.com/1.1/statuses/update\_with\_media.json isError false requestId userdata: 0x7ff4b18e8ff8

The error message “Error creating status” is a bit vague, and after searching I haven’t found any definitive answers on why that is.

I can send a regular tweet just fine, so I don’t think the authorisation process is the problem, but I could be wrong.

Hi mpappas, I used your method far my facebook posts and it looks fine to me, so thank you.

I’m still no closer with posting images to Twitter if anyone is out there?

Hmm… I have some old code laying around… It says something about android, so, no idea how it works (it looks like it just uses a url, so maybe the sdk packages the image when sending)… or if it even works at all (iOS is iffy from that if statement… but you can give it a try, it looks generic)

Anyways, Caveat Emptor

[lua]

    if( utils.isAndroid == true ) then      
       
        local tweet = utils.urlEncode(mojoGroups.currentMenu.PTITLE … " - " … currMess.MESSAGE … “\n------\nPosted from localNet”)    
        local tweetURL = “https://twitter.com/intent/tweet?text=” … tweet
        
        if( imageFilenameLarge ~= nil ) then
            if( imageFilenameLarge ~= “” ) then
                tweetURL = tweetURL … “&url=” … utils.urlEncode(imageFilenameLarge)
            end
        end    
        
        print(" — Sending twitter post request…", tweetURL)    
        native.showWebPopup( tweetURL )             – Full screen…
 

[/lua]

Note that the image name is (I believe) a full pathname on the device… (system.pathForURL type, not just a base filename)… Oh, and you’ll need to URL encode the filename, since it is a full path (masks the slashes in the url when sending filenames, etc)

i just tried it out this morning, and it doesn’t work I’m afraid.  

When I try this:

local util = require("util-1") local tweet = urlencode("Hello world") local tweetURL = "https://twitter.com/intent/tweet?text=" .. tweet local imageFilenameLarge = system.pathForFile("myImage.jpg", system.DocumentsDirectory) if( imageFilenameLarge ~= nil ) then if( imageFilenameLarge ~= "" ) then tweetURL = tweetURL .. "&url=" .. urlencode(imageFilenameLarge) end end native.showWebPopup( tweetURL ) 

I see this url:

https://twitter.com/intent/tweet?text=Hello%2bWorld&url=%2fUsers%2falan%2fLibrary%2fApplication%2bSupport%2fCorona%2bSimulator%2fCorona%2d909D2B1AC113E532C469828EFD8422BE%2fDocuments%2fmyImage%2ejpg  

which looks correct, but when I post it to twitter via the web popup it doesn’t post the image in any way, just the text part of the tweet.

If you have your app setup on Twitter, then using the “TwitterFacade” library should work well.

You have to pay for it, but its worth it i think: http://silverthorax.com/libs/twitter/

I saw that, but I also saw this post:

http://forums.coronalabs.com/topic/37826-twitter-facade-compatibility-issue/

with people claiming it doesn’t work any more.

I downloaded and used it only last week and haven’t had a single problem. Maybe i was just lucky!

Alternatively GGTwitter from Glitch games also posts Images, but i haven’t tried that out yet. 

woops, sorry about the twitter pic alan – I was just debugging something and saw the twitter url in the sim… my app is actually passing a web url, not a local device url… In my case, the users photo is being loaded to a server, and the url for that is what is passed to twitter… Could be any web url I suppose, but it’s definitely not from the device in the case of my twitter code above. Sorry bout that.

@TandG, I bought TwitterFacade and you were quite right, it does work.

Thanks for the advice everyone.

I’ve got this code which posts either photos or videos to fb albums (after the app has used the sdk to login to fb of course)

[lua]

           if( isPhotoPost == true) then
                if( (utils.isEmpty(imageFilenameShare) ~= true) and (this.sharePopup.MessageButton.text.text == “On”)) then             – They want the composite image, right?
                    fileLocation = imageFilenameShare       – Special image with text composited into it…         
                else                                                        
                    fileLocation = utils.getBaseFilename(imageFilenameLarge)
                end
            
            – New method of posting it up… As a FB photo… (Not as a stream post)
            
                local attachment = {
                        message = currMess.MESSAGE,
                        source = {
                                baseDir=system.TemporaryDirectory,
                                filename=fileLocation,
                                type=“image”
                        },
                    }
                facebook.request( “me/photos”, “POST”, attachment )    
            else
                – else video— looks simple, but there was a lot of monkey business discovering undocumented corona technique to post video to fb…
                fileLocation = utils.getBaseFilename(this.videoName)
                    
                local uploadParams = {}
                uploadParams[fileLocation] = {            
                    baseDir=system.TemporaryDirectory,
                    filename=fileLocation,
                    type=“video/mp4”               
                }
                
                uploadParams.title=tostring(mojoGroups.currentMenu.PTITLE)
                uploadParams.description=tostring(currMess.MESSAGE)
                
                print(" – sending fb request")
                facebook.request( “me/videos”, “POST”, uploadParams )  

[/lua]

I can’t recall if I tried just putting it in the feed first (my comment sort of indicates I changed later to posting into the albums). But in this case, there’s no base64 stuff or hosting somewhere else, etc. Also, I believe fb returns a photo id reference/number when photos are posted into the users album this way. Maybe the returned facebook object ID  (returned from the facebook.request() call above) could be used as the online reference for a feed post, as you mentioned.

Thank you mpappas, I want to post straight to the feed rather than to an album, but I’ll take a look later and see if I can work this to fit my needs.

I found a site that had an example of posting images to twitter supposedly working: 

http://velluminteractive.com/upload-images-to-twitter-from-corona-sdk/

but it would seem it’s outdated and no longer works. For a start it’s still using the “upload.twitter.com/1/” url rather than “api.twitter.com/1.1/”.

Does anyone at Corona have any experience with posting images to Twitter, I suspect it’s probably quite simple for someone more experienced?

update_with_media has been part of the Twitter api for a few years now as far as I can tell, so I’m surprised it’s not in the sample code already tbh.

Here’s the response data I receive:

responseHeaders : Content-Type application/json;charset=utf-8 Date Thu, 27 Feb 2014 19:55:36 GMT x-xss-protection 1; mode=block x-mediaratelimit-class photos Expires Tue, 31 Mar 1981 05:00:00 GMT Last-Modified Thu, 27 Feb 2014 19:55:36 GMT status 403 Forbidden Pragma no-cache Server tfe x-content-type-options nosniff Content-Length 83 strict-transport-security max-age=631138519 x-mediaratelimit-reset 1393616856 Content-Encoding gzip x-transaction dc72366e2ce6319e Cache-Control no-cache, no-store, must-revalidate, pre-check=0, post-check=0 x-access-level read-write x-frame-options SAMEORIGIN x-mediaratelimit-limit 1000 x-mediaratelimit-remaining 996 responseType text phase ended bytesEstimated -1 rest of data: response {"errors":[{"code":189,"message":"Error creating status."}]} name networkRequest bytesTransferred 60 status 403 url https://api.twitter.com/1.1/statuses/update\_with\_media.json isError false requestId userdata: 0x7ff4b18e8ff8

The error message “Error creating status” is a bit vague, and after searching I haven’t found any definitive answers on why that is.

I can send a regular tweet just fine, so I don’t think the authorisation process is the problem, but I could be wrong.

Hi mpappas, I used your method far my facebook posts and it looks fine to me, so thank you.

I’m still no closer with posting images to Twitter if anyone is out there?

Hmm… I have some old code laying around… It says something about android, so, no idea how it works (it looks like it just uses a url, so maybe the sdk packages the image when sending)… or if it even works at all (iOS is iffy from that if statement… but you can give it a try, it looks generic)

Anyways, Caveat Emptor

[lua]

    if( utils.isAndroid == true ) then      
       
        local tweet = utils.urlEncode(mojoGroups.currentMenu.PTITLE … " - " … currMess.MESSAGE … “\n------\nPosted from localNet”)    
        local tweetURL = “https://twitter.com/intent/tweet?text=” … tweet
        
        if( imageFilenameLarge ~= nil ) then
            if( imageFilenameLarge ~= “” ) then
                tweetURL = tweetURL … “&url=” … utils.urlEncode(imageFilenameLarge)
            end
        end    
        
        print(" — Sending twitter post request…", tweetURL)    
        native.showWebPopup( tweetURL )             – Full screen…
 

[/lua]

Note that the image name is (I believe) a full pathname on the device… (system.pathForURL type, not just a base filename)… Oh, and you’ll need to URL encode the filename, since it is a full path (masks the slashes in the url when sending filenames, etc)

i just tried it out this morning, and it doesn’t work I’m afraid.  

When I try this:

local util = require("util-1") local tweet = urlencode("Hello world") local tweetURL = "https://twitter.com/intent/tweet?text=" .. tweet local imageFilenameLarge = system.pathForFile("myImage.jpg", system.DocumentsDirectory) if( imageFilenameLarge ~= nil ) then if( imageFilenameLarge ~= "" ) then tweetURL = tweetURL .. "&url=" .. urlencode(imageFilenameLarge) end end native.showWebPopup( tweetURL ) 

I see this url:

https://twitter.com/intent/tweet?text=Hello%2bWorld&url=%2fUsers%2falan%2fLibrary%2fApplication%2bSupport%2fCorona%2bSimulator%2fCorona%2d909D2B1AC113E532C469828EFD8422BE%2fDocuments%2fmyImage%2ejpg  

which looks correct, but when I post it to twitter via the web popup it doesn’t post the image in any way, just the text part of the tweet.

If you have your app setup on Twitter, then using the “TwitterFacade” library should work well.

You have to pay for it, but its worth it i think: http://silverthorax.com/libs/twitter/

I saw that, but I also saw this post:

http://forums.coronalabs.com/topic/37826-twitter-facade-compatibility-issue/

with people claiming it doesn’t work any more.

I downloaded and used it only last week and haven’t had a single problem. Maybe i was just lucky!