Twitter Plugin is Available Now!

Hey everybody,

Just a public service announcement that my Twitter plugin, which is free, is up on the Corona Labs plugin store (store.coronalabs.com) and can be used starting today. If you have any interest in integrating Twitter into your apps, I think you may find it very useful, and hopefully easy to use.

Full documentation on the plugin, plus a downloadable sample project, can be found at my website: http://twitter.plugin.jasonschroeder.com.

I created the Twitter plugin for Corona SDK to give Corona developers an easy way to access the full range of Twitter’s REST APIs from inside their apps, making it simple to get data from or post information to a Twitter user’s account with as little as a single line of code. The plugin automatically handles oAuth authentication, and persistently saves a logged-in user’s token data, so they’ll only need to log in and authorize your app once, even across multiple sessions, until they are manually logged out or revoke your app’s access to their Twitter account. The plugin also comes with a set of “convenience functions” that return Lua tables and/or Corona display objects, for easier access to common-use applications without the need for developers to wade through the raw JSON data that Twitter’s REST APIs return.

Here’s a video of the sample project, which gives you some idea of how it works and what it can be used for:

https://www.youtube.com/watch?v=Eg7N_f1BoTQ

Excellent to see this up. Thank you for making this.

Good work! I’ll have to give this a go soon!

Thanks for sharing…

This is an amazing plug-in and so simple to use, love it.

Is it possible to add callbacks for failed/cancelled/etc? I have UI elements I need to reset and other logic to perform if the user does not complete the log-in for whatever reason.

Great work!

Thanks, @BearHug! And that’s a great suggestion!

I just pushed out an updated version of the plugin. It could take an hour or more for it to be live on the distribution server, but once it is, here’s what’s new:

  • twitter.login() now accepts two arguments: an “onSuccess” function (which takes the place of the former “callback” function) and an “onFail” function that gets triggered if a user fails to log in (bad password, etc.) or if they choose to cancel the login attempt.

Hopefully this’ll give you what you’re looking for. I’ll update the documentation on my own site later today, but here’s some sample code to show what I mean:

local twitter = require "plugin.twitter" twitter.init("API\_KEY", "API\_SECRET") local function onSuccess() print("Twitter user " .. twitter.user.userName .. " successfully logged in!") end local function onFail() print("Twitter login attempt failed or was cancelled by user.") end twitter.login(onSuccess, onFail)

Thanks again for using the plugin. Enjoy!

That’s exactly what I’m looking for, brilliant work! Hugely appreciated, thank you.

UPDATE: the plugin now supports the Windows simulator! Previously it did not, because the only way to log in and authorize a Twitter app is through a webpage and URL listener, and the Windows simulator does not support native.newWebView().

Now the workaround is a bit of a hack, and does involve an extra step or two, so it’s by no means perfect. But it will allow you to test out your Twitter-centric code in the Windows simulator without needing to build for device. You can read detailed instructions for the workaround at http://www.jasonschroeder.com/2015/07/15/twitter-plugin-for-corona-sdk/#gotchas. Or just call twitter.login() from the Windows simulator and it will walk you through the workaround process.

Thank you, very cool!

Jason,

Thanks for donating this powerful and easy to use Plugin to the community - I was able to send out a tweet in 10 lines of code!!!

Do you have any plans to add any other convenience functions?

In particular, I am sure the community would love the ability to add an image along with the tweet text (I know this is also available with the Social Plugin ) - is this on your wish list already?

I had a quick look at the Twitter Restful Api and looked at using your twitter.request() call to call the POST media/upload endpoint but got lost with the multipart/form-data requests.

Keep up the great work!

Hi Anil,

Thanks for the kind feedback! I honestly hadn’t spent too much time with uploading photos, so I had left it out of the “convenience functions” but I’ll see what I can do. I agree that it’d be a nice addition, and something that (if I do it right) could really take some of the frustration out of the process. I’m a little busy with client projects right now, so I might not be able to add this feature as fast as I’d like, but it’s on my radar.

Thanks!

Jason,

I am sure that this new convenience function is going to make a lot of people happy - I will be keeping a keen eye out for it in the coming weeks.

Don’t want to sound cheeky, but if you are keeping a wish list for the plugin, then Twitter also supports adding your current location to the tweet which effectively gives you a ‘Checking In’ type facility similar to Facebook and Foursqare et al (   https://dev.twitter.com/overview/api/places and https://support.twitter.com/articles/122236). 

Thanks again.

Anil

Hi Anil,

I’ll give some thought to adding support for adding location using twitter.tweet(), but my initial thought is to leave that functionality to twitter.request(), as I don’t want to over-complicate the convenience functions too much. But you can certainly add a location easily enough using twitter.request(). Use the “statuses/update” endpoint and add an optional “place_id” parameter in the parameters table.

UPDATE TO PLUGIN SUPPORT ON WINDOWS SIMULATOR:

Since Corona Labs added support for native.newTextField() in the latest daily build, I have modified the way the plugin behaves in the Windows simulator - whereas previously you had to copy and paste a valid oAuth token string into your code and relaunch the simulator every time you called twitter.login(), now you are presented with a text field when you call twitter.login() and you can just paste that token string directly into the simulator, with no need to modify your code!

The only “gotcha” with this new method is that you must be using daily build # 2015.2695 or later to use the plugin in the Windows simulator. I decided that the hassle of requiring plugin users to update their SDK was worth it, given the much faster and easier authorization process with this new method. Note that you can still build for device using earlier versions of the SDK with no issues - you only need to update your SDK if you want to test plugin functionality in the Windows simulator. If you do try to call twitter.login() using an earlier SDK build, you’ll get a native system alert reminding you that you must update to use the plugin in the Windows simulator.

I’ve updated the documentation on my website and have pushed the new plugin version live, so you should see this new functionality next time you fire up the plugin in the Windows simulator. Enjoy!

So I’m trying to upload a photo to twitter using the twitter.request function but getting this error:

Your twitter request was received successfully, but returned the following error(s): CODE 195: Missing or invalid url parameter.

v1 :

twitter.request({ endpoint = "statuses/update\_with\_media", method = "POST", parameters = { status = "test tweet", media = "pic.png", }, callback = myCallback, })

v2 :

twitter.request({ endpoint = "statuses/update\_with\_media", method = "POST", url = "https://api.twitter.com/1.1/statuses/update\_with\_media.json", contentType = "multipart/form-data", parameters = { status = "test tweet", media = "pic.png", }, callback = myCallback, })

v3 :

twitter.request({ endpoint = "statuses/update\_with\_media", method = "POST", parameters = { status = "test tweet", media = "pic.png", url = "https://api.twitter.com/1.1/statuses/update\_with\_media.json", contentType = "multipart/form-data", }, callback = myCallback, })

ps this works :

twitter.request({ endpoint = "statuses/update", method = "POST", parameters = { status = "test tweet", }, callback = myCallback, })

v4 :stuck_out_tongue:

baseDir = system.DocumentsDirectory

display.save( backGroup, “screenshot.png”, system.DocumentsDirectory )

local function read_image()

local path = system.pathForFile( “screenshot.png”, system.DocumentsDirectory )

local f = assert(io.open( path ))

local image_data = f:read("*a")

f:close()

return image_data

end

twitter.request({

endpoint = “statuses/update_with_media”,

method = “POST”,

parameters = {

    status = “Hello World From Lua!” … os.time(),

    --[“media”] = 

    [“media[]”] = {

filename = “@screenshot.png”,

data = read_image()

}

},

callback = myCallback,

})

but still getting the Your twitter request was received successfully, but returned the following error(s):

CODE 195: Missing or invalid url parameter.  error, not sure what to do 

@mmk: I don’t think the current version of the plugin is going to do what you want it to. Because the plugin does not handle converting image files into multipart form data (which Twitter requires), you’re going to keep running into this issue. I’m working on a solution, but it’s going to take me some time, sorry. Adding to the confusion is the fact that the “statuses/update_with_media” endpoint is deprecated, so I’m not sure how well it’s going to work, if at all. And any other Twitter modules I’ve seen for Corona have used that specific endpoint to handle media uploads. It’s a sticky wicket, that’s for sure. Sorry to ask you to wait, but I’ll see what I can do. It’s going to be several days, at least - and possibly longer, I’m afraid, before I’ll be able to dedicate enough time to this to properly identify and implement a solution, given my schedule constraints at the moment.

i see no worries ill will wait :D 

Well, despite my warnings that it would take me a while to implement this, I got a bee in my bonnet about it yesterday and wound up adding support for image uploads to the plugin! I’ve updated the documentation on my website (http://twitter.plugin.jasonschroeder.com) but here’s a quick primer on how to upload images using twitter.tweet and/or twitter.request:

Adding an image to a twitter.tweet() call is super easy. Just pass in an image file like you would for display.newImage() as the second argument to twitter.tweet() and it’ll upload that picture and attach it to your tweet. If you are specifying a callback function (which used to go in the second argument slot), just make that the third argument now - unless you don’t want to upload an image, in which case you can use the old syntax, where the callback function is the 2nd argument. For example, this bit of code:

[lua]twitter.tweet(“Tweeted from @schroederapps #CoronaSDK Twitter plugin. Now supporting image uploads!”, “yes.jpg”, callbackFunction)[/lua]

will result in this tweet:

tweetImgSample.png

Or, if you’d rather take more control and use twitter.request, then this bit of code would result in the same tweet getting posted (but you’d get access to the full returned JSON in your callback function):

[lua]twitter.request({

   endpoint = “statuses/update_with_media”,

   method = “POST”,

   parameters = {

      status = “Tweeted from @schroederapps #CoronaSDK Twitter plugin. Now supporting image uploads!”,

      media = system.pathForFile(“yes.jpg”, system.ResourceDirectory),

   },

   callback = callbackFunction,

})[/lua]

Enjoy!

works like a charm :smiley: