Twitter Plugin is Available Now!

if you want to tweet your level or score :smiley:

showLevel = settings.get('levelSelect') -- level currently playing twitter.request({    endpoint = "statuses/update\_with\_media",    method = "POST",    parameters = {       status = "im struggling with level " .. showLevel .. " playing ...... bla bla bla link",       media = system.pathForFile("screenshot.png", system.DocumentsDirectory),    },    callback = tweetCallback, })

Seems great! Is this available for Enterprise users as well?

Hi schroederapps,

Thank you for this great plugin!

There is a major bug with the Twitter login though. If you have a username (screenName) with an underscore ā€œ_ā€ (Aidan_Wolf), you get a Runtime Error:

Runtime error  ?:0: attempt to concatenate field 'screenName' (a nil value) stack traceback:  [C]: ?  ?: in function '?'  ?: in function \<?:164\>

Please fix!

And while Iā€™m at it, do you think you could integrate the quick twitter login feature on iOS? The popup that displays your currently connected accounts.

Thank you,

Aidan

Hi @AidanWolf,

Thanks for the kind words. I was able to login successfully and utilize the full range of APIs with an account that has underscores in its name (@screen_sat_app), so I think there might be an issue with your code that is preventing it from working. The runtime error you pasted indicates that the screenName was nil, which leads me to believe that you are not putting the screen name in quotes when making an API call - am I right in that assumption?

When making an API call that takes a screen name as an argument, that screen name must be a string - meaning it has to be in quotes, or it must be a variable name that references a string value. For example, here are two valid ways of getting basic user information for your twitter handle:

--passing a string directly into the API call: twitter.getUser("Aidan\_Wolf", callback)

-- passing a variable into the API call that references a string: local myScreenName = "Aidan\_Wolf" twitter.getUser(myScreenName, callback)

Let me know if you still canā€™t get it to work, and please attach a simple lua file that illustrates exactly how you are getting the error and Iā€™ll investigate further if need be. But I suspect that a simple modification to your code will fix the issue.

As to your second request, Iā€™m afraid that because I wrote this plugin as a pure Lua module, Iā€™m unable to access native OS features like the one you mentioned. An enterprise developer might be able to do that, but itā€™s not something Iā€™m able to do at this time. Sorry. Note that if you simply want to post a tweet to the userā€™s (already logged-in) Twitter account, you can do that using the built-in Corona social plugin. My Twitter pluginā€™s primary value is that it allows you to pull data from a Twitter account - posting simple tweets was already possible via the social plugin.

Thanks for using the plugin - good luck with your app! :slight_smile:

Thank you for the quick reply.

This is all the code required:Ā 

local twitter = require("plugin.twitter") twitter.init("xxx", "xxx") local function onSuccess() print("successful!") end local onFail function onFail() print("Twitter login attempt failed or was cancelled by user.") end twitter.login(onSuccess, onFail)

Response for ā€œAidan_Wolfā€

Runtime error  ?:0: attempt to concatenate field 'screenName' (a nil value) stack traceback:  [C]: ?  ?: in function '?'  ?: in function \<?:164\>

Response for ā€œCapsuleDevā€

Successfully authorized Twitter user "CapsuleDev" Jan 24 02:21:39.358 successful!

In your documentation, there is an error with the code, possibly needs updated?

print(ā€œTwitter user " ā€¦ twitter.user.userName ā€¦ " successfully logged in!ā€)

Switching ā€œuserNameā€ to ā€œscreenNameā€ fixed it. I can imagine thereā€™s something in the code that is not updated to handle ā€œscreenNameā€ when thereā€™s some type of special formatting necessary.

@AdianWolf - dā€™oh! You are right. I had a typo in one section of my documentation where I listed ā€œuserNameā€ instead of ā€œscreenNameā€ in an example. Itā€™s been fixed. Thanks for helping find that!

Hi Jason,

Thanks creating this plugin! Ā I having an issue where the twitter auth popup is incredibly off centered when you call twitter.login() outside of main.lua Ā You can only see 1/4 of the UI. Ā If I put the call in main.lua, it gets displayed just like in your demo.

Iā€™m using Coronaā€™s composer/scenes to organize my UI. Ā Do you have a fix or workaround for this? Ā 

Ben

Hi @Ben:

Iā€™ve never heard of this, and itā€™s not something Iā€™ve seen. There shouldnā€™t be any reason why you canā€™t use the plugin within a composer scene. Are you able to put together a small demo project that illustrates the problem? If so, I can try to find what might be causing it and/or adjust the plugin if something in the plugin is to blame.Ā 

Thanks,

Jason

@jason thanks for your reply. Ā I found the problem. Ā I changed the default anchor when building one of my UIs.

display.setDefault( ā€œanchorXā€, 0 )

display.setDefault( ā€œanchorYā€, 0 )

Ah, that makes sense. Still - I appreciate you mentioning it here. I can explicitly set the anchorX and anchorY of the webView to .5 to avoid this in the future. :slight_smile:

Hi how can i trigger a function on a TWITTER PLUGIN ERROR: like :

if error thenĀ 

twitterbutionback()

end

@mmk.verheijden: thatā€™s not a feature of the plugin at the moment, but itā€™s a good idea. Iā€™ll add it to the ā€œto-doā€ list and try to work it into a plugin update ASAP.

thanksĀ jason

@mmk: I added the ability to listen for errors in your callback functions and respond accordingly. I just pushed the new version of the plugin, but it could be a few hours before itā€™s actually on Coronaā€™s build servers for your use. Hereā€™s details on the new error handling functionality:Ā http://www.jasonschroeder.com/2015/07/15/twitter-plugin-for-corona-sdk/#errorHandling

Essentially, callback functions previously only took one argument - now they take up to 3: the same old ā€œresponseā€ (JSON string or Lua table), a ā€œisErrorā€ boolean (nil if no error, true if there was an error), and ā€œeventā€ - a standard Corona networkResponse event table (nil if no error). Hope this helps - thanks for the suggestion!

@benchow: I also added a fix for the off-centered login webview if you changed the default anchorX and anchorY values. Thanks again for reporting that!

@mmk thank you for asking.

@schroederapps thank you for implementing this feature, it was a must have, especially when hitting rate limits or getting blocked by the api (unknowingly for a period of time.)Ā 

If any of you are interested, I can post my function for defaulting to the native social plugin in an error situation.

trying to create a demomy code below

local myRectangle = display.newRect( display.contentCenterX, display.contentCenterY, display.actualContentWidth, display.actualContentHeight )

local twitter = require (ā€œplugin.twitterā€)

twitter.init(ā€œgWxZVU4RW56ss2VIGnoiem0M2ā€, ā€œfPd7s0raTVmE4YxqvLzR6wT4ZEyPuPuefgEuWGguwzgf8dhL4fā€)

local myText = display.newText( ā€œTwitter Test Appā€, display.contentCenterX, 5, native.systemFontBold, 12 )

myText:setFillColor( 1, 0, 0.5 )

local widget = require( ā€œwidgetā€ )

ā€“Log In

local function onSuccess()

Ā  Ā  print(ā€œaaā€)

print(ā€œTwitter user " ā€¦ twitter.user.screenName ā€¦ " successfully logged in!ā€)

myText:removeSelf( )

myText = nil

myText = display.newText( ā€œTwitter user " ā€¦ twitter.user.screenName ā€¦ " successfully logged in!ā€ , display.contentCenterX, 5, native.systemFontBold, 12 )

myText:setFillColor( 1, 0, 0.5 )

end

local function onFail()

print(ā€œTwitter login attempt failed or was cancelled by user.ā€)

myText:removeSelf( )

myText = nil

myText = display.newText( ā€œTwitter login attempt failed or was cancelled by user.ā€ , display.contentCenterX, 5, native.systemFontBold, 12 )

myText:setFillColor( 1, 0, 0.5 )

end

local function logInEvent( event )

Ā  Ā  if ( ā€œendedā€ == event.phase ) then

Ā  Ā  Ā  Ā  print( ā€œButton was pressedā€ )

Ā  Ā  Ā  Ā  twitter.login(onSuccess, onFail)

Ā  Ā  end

end

local button1 = widget.newButton(

Ā  Ā  {

Ā  Ā  Ā  Ā  left = 100,

Ā  Ā  Ā  Ā  top = 100,

Ā  Ā  Ā  Ā  id = ā€œbutton1ā€,

Ā  Ā  Ā  Ā  label = ā€œTwitter loginā€,

Ā  Ā  Ā  Ā  onEvent = logInEvent

Ā  Ā  }

)


ends up with

ERROR: Runtime error
?:0: attempt to concatenate field ā€˜screenNameā€™ (a nil value)
stack traceback:
[C]: ?
?: in function ā€˜?ā€™
?: in function <?:164>

ive been using the twitter demo app to returns same error

Hi Jason,

I just wanted to say that Iā€™ve been working with your Twitter plugin -Ā https://marketplace.coronalabs.com/plugin/twitter - for the last couple of days and wanted to say a big thank you for your contribution to the Corona community! Itā€™s been a real time saver and itā€™s well worth the $3 asking price on the marketplace (if not more)!

With that, I have a request or twoā€¦ hope you donā€™t mindā€¦ :slight_smile:

  1. The method twitter.tweet() successfully returns the id of the newly posted tweet for a simple status update but does not when imageFile is provided - even though the tweet and image file have both been sent successfully. Is this something that can be corrected please?
    Ā 
  2. I see that the endpoint " statuses/update_with_media" has been deprecated in favour of using " media/upload" then " statuses/update" but I was unable to achieve this myself using the twitter.request() method. Would it be possible to provide a code sample please on how this might be achieved with the plugin (especially ā€œEnsure the POST is a multipart/form-data requestā€)?
    Ā 
  3. The demo project didnā€™t work for me Iā€™m afraid - the buttons never responded - so I decided to dig in to the code. I wonder, would it be possible to provide a console-only demo where one can just comment/uncomment methods they want to test and configure please? I found mixing these methods with presentation logic a little confusing.

Again, many thanks for such a great plugin and I look forward to seeing it thrive on the marketplace!

Thanks,

AppDevGuy

@AppDevGuy: thanks for the kind words! Iā€™ll look into your requests. At first glance, #1 (returning the ID) seems easily doable. #2 is a lot trickier, as handling multipart form data is not the easiest thing and itā€™s been a while since Iā€™ve dealt with it. Iā€™ll see what I can do, but as I recall, I opted to use the update_with_media endpoint for my convenience function precisely because getting media/upload to work from Corona was not the simplest of operations. For #3, if Iā€™m being honest, Iā€™m not very likely to put together another demo project anytime soon - I downloaded a copy, plugged in a valid API key/secret pair and it just worked for me, on both Mac and Windows. Are you using a recent daily build of the SDK? If you need basic code samples, there are code snippets for each method in the documentation at http://twitter.plugin.jasonschroeder.com.

Stay tuned for a fix on that first request for certain, though. :slight_smile:

Thatā€™s great, thanks Jason! :slight_smile:

#1 : Would it also be possible to return the full Twitter response too I wonder? As though one might have called the .request method themselves? Perhaps have a third property - a ā€œdetailsā€ table - in the response along with id and text that contains this. That would be very useful if possible.

#2 : Yeah, I had a quick go myself with network.request and I could see it would require more work than the time I have to spare at the moment. Good luck with it! Hopefully ā€œupdate_with_mediaā€ will be with us for a little while longer yet.

#3 : Hmmā€¦ I thought I was. Iā€™ll try again. As for samples, it might be useful if we users share code snippets on here as we dig through the Twitter api and put together custom requests. Iā€™ll be more than happy to do that once I experiment beyond basic usage.

Thanks again, Jason. Yours is a vital plugin for Twitter engagement and I do hope it receives the support and recognition it deserves.