Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem With Twitter Plugin
Started by anil8 Oct 02 2015 03:33 PM

17 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Hi Jason,

 

 

There seems to be an issue with the tweet convenience function when used with an attached image.

 

If I call the tweet function without an image then all is fine and the tweet is posted and the call back function is called e.g.

local function tweetCallback( response )

  print("TWEET TEXT: " .. response.text)
  print("TWEET ID: " .. response.id)

end

twitter.tweet( "A test tweet", tweetCallback )

However, if I now add an image to the function call then the tweet is successfully posted to twitter and indeed has the image in the tweet but I get an error in the simulator and the callback function is not called eg.

local function tweetCallback( response )

  print("TWEET TEXT: " .. response.text)
  print("TWEET ID: " .. response.id)

end

twitter.tweet( "A test tweet", "assets/myimage.jpg", tweetCallback )

The error that I get in the simulator is :

Runtime error
	?:0: attempt to index a nil value
stack traceback:
	[C]: ?
	?: in function <?:248>

I am using a daily build number 2015.2716 (2015.9.14) on a Windows 7 PC.

 

Hope you can help.

 

 

Thanks

 

 

Anil

 



[TOPIC: post.html]
#2

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

Hi Anil,

 

I created a very simple test app to test out the problem you're having, and it worked as expected for me. There might be an issue with your code unrelated to the Twitter plugin that is causing the issue? Here is a very simple main.lua that I whipped up by copying and pasting your own code (except I changed the path to an image file) that worked for me. Can you please try this out and let me know if it works for you? If so, then the issue isn't plugin-related, at least not directly.

 

MAIN.LUA:

local twitter = require("plugin.twitter")
twitter.init("XXX", "XXX") -- replace those XXXs with your Twitter app's API key & secret

local function tweetCallback( response )

  print("TWEET TEXT: " .. response.text)
  print("TWEET ID: " .. response.id)

end

twitter.tweet( "A test tweet", "test.png", tweetCallback )

When I run that in the simulator here's what I see in the terminal:

Oct 02 09:59:18.124: TWEET TEXT: A test tweet http://t.co/DaxJIcTRlr
Oct 02 09:59:18.124: TWEET ID: 650127954631086080

Please try that out and report back. I'm happy to help if I can.



[TOPIC: post.html]
#3

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Hi Jason,

 

 

Still the same issue and I have even used your failwhale.png graphic.

 

What version of Corona are you running?

 

I have attached a screenshot of the console as well as my twitter feed so you can see that the tweet is being sent.

 

Thanks

 

Attached File  twittererror.png   606.57KB   0 downloads

 

EDIT:

 

Just checked out the changelog on your web site and see that somebody else reported the same issue but was put down to possibly being a scoping issue but obviously with the empty main.lua this can't be the case :

 

Seye Ayinla
August 29, 2015 at 6:46 am | Permalink

Hi Jason. Nice API by the way but I seem to be having an issue with the twitter.tweet() function call. The problem is an error pops up that halts the progress only after a tweet with a picture.
Don’t know what I’m specifically doing wrong.

The tweet gets posted but doesn’t fire my function call back for a reason. And the error I keep getting is

Runtime error
?:0: attempt to index a nil value
stack trackback:
[C]: ?
?: in function

Strange as it only happens with the pictures. But the tweet gets posted so I really don’t know. Been on it for 3 hrs now still no result.



[TOPIC: post.html]
#4

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

HI Anil,

 

I was running that main.lua on a Mac yesterday. I pulled out my Windows machine today and I get the same error as you. Likewise, the demo project (which ran fine on a Windows machine a few months ago) is running into weird Runtime errors on the most recent daily build. I'm not sure what's going on yet, but it's definitely only on Windows as far as I can tell. If you have time, please try a device build of your app and let me know if things are working there as expected. I'm going to try downloading older Corona builds to pinpoint at which point something changed in the Windows simulator that is causing these errors. We'll get to the bottom of it eventually - please be patient with me in the meantime.

 

Thanks,

Jason



[TOPIC: post.html]
#5

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Hi Jason,

 

 

I have just built the app for my device and unlike the Simulator where I would get a console error but the tweet would go through, on the device the tweet does not go through at all.

 

Thanks



[TOPIC: post.html]
#6

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

Well this just gets stranger and stranger. What type of device are you running this on? Can you provide a device error log?

 

Thanks,

Jason



[TOPIC: post.html]
#7

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Jason,

 

 

I tested on a Kindle Fire HD7 as that was the device I had to hand but will build it on a HTC and a Samsung phone later today and let you know the results.

 

Unfortunately, I don't have Android debugging and the SDK Manager loaded so can't give you a device error log.



[TOPIC: post.html]
#8

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Hi Jason,

 

 

Unfortunately I also get the same issue when I try to send a tweet with an image using both the HTC and Samsung phones. Tweets without images get sent fine.

 

Do you know which version of Corona SDK is best to use - I don't need to use the latest daily buiild.

 

 

Thanks

 

Anil



[TOPIC: post.html]
#9

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Hi Jason,

 

 

Have you been able to get to the bottom of this yet?

 

I have tried rebuilding my app using the recent public build and still am unable to tweet with an image.

 

In fact, even building the simplest app with just your sample main.lua below does not work either.

local twitter = require("plugin.twitter")

twitter.init("XXX", "XXX") -- replace those XXXs with your Twitter app's API key & secret

local function tweetCallback( response )

  print("TWEET TEXT: " .. response.text)
  print("TWEET ID: " .. response.id)

end

twitter.tweet( "A test tweet", "test.png", tweetCallback )

Thanks



[TOPIC: post.html]
#10

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

Hi Anil,

Sorry. I had some contract work pop up unexpectedly that is taking up my dev time. I'll do my best to sort things out ASAP. Stay tuned. Might have to be later this week but I'll get to the bottom of it.

[TOPIC: post.html]
#11

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Hi,

 

 

Congrats on your new job and love your work on the BeeAmazed app!

 

Hate to ask but any news on the Twitter bug?

 

 

Thanks



[TOPIC: post.html]
#12

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

@Anil: thanks for checking in. No fix yet, but I'm hoping to spend some time with it tonight. Things have been a bit busy lately, which is a flimsy excuse, I know, but I so appreciate your patience. (And thanks for the kind words about BeeAmazed!)

 

Stay tuned - I hope to get this thing fixed for you ASAP.



[TOPIC: post.html]
#13

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

Hi Anil,

 

Just confirming that I hope to have this worked out this week for sure, and hopefully in the next day or two. Life has been conspiring to keep me away from the Twitter plugin, but I'm pushing back big time this week!

 

Thanks,

jason



[TOPIC: post.html]
#14

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Thanks for the update Jason - you're a star!!!



[TOPIC: post.html]
#15

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

@Anil - I'm happy to report that the issue is FIXED and I've pushed an updated version of the plugin to Corona's build servers. It could take an hour or two before the update actually makes its way to you via the Simulator, but it should be all fixed now. Let me know if that's not the case.

 

In case you're interested, the issue was that the JSON string that gets returned by Twitter when you post a tweet was getting corrupted on Windows and Android devices when you included an image. Not sure why it happened, and honestly I implemented a bit of a hacky fix (if valid JSON isn't returned, I fake an empty lua table in its place). But it stops that Runtime error, and your tweets do get posted. The downside is that it doesn't pass a valid tweet ID into your listener function, because I have no way of retrieving that from the corrupted JSON string. I'm trying to work with Corona to figure out why Windows and Android don't get the same valid response as OS X and iOS, but for now it's better than it was. No Runtime errors or crashes! :)

 

As for why you couldn't post an image on your Android device, that's nothing to do with the plugin, but rather it's a limitation of the access we get to the system.ResourceDirectory on Android - but fear not, I've got a fix for you. Will detail that in a second post momentarily...



[TOPIC: post.html]
#16

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

Okay, so there have been numerous forum posts in the past about getting local HTML files to load in a webview on Android. It's traditionally been a huge pain because for whatever reason Android wouldn't read files from the system.ResourceDirectory. I believe that Corona has managed to fix this so that opening local webpages is nice and easy. HOWEVER, when I tried posting a tweet with an image attached on my own Android device, I got an error message saying that it couldn't find the image I wanted to upload, even though it was sitting right there in my app's resource directory. This seemed familiar, so I tried using a workaround that I used last year before Corona fixed the whole HTML thing.

 

The big thing to remember here is that images that are in your system.DocumentsDirectory can be attached to tweets just fine. So if you're trying to attach an image that you downloaded to system.DocumentsDirectory, or one that you generated within the app via display.capture(), then you shouldn't have any problems. BUT - if you want to attach an image that resides in your resource directory, you've got to copy that file over manually. And to make matters worse, for whatever reason, it doesn't work with images that have .jpg or .png file extensions. But if you put an image file in your resource directory and change its file extension from .jpg or .png to .txt, then you can copy that file to your documents directory and rename it with the right file extension, and then you can attach it to your tweet! I know it's a pain, but I tested it using the system below and it worked for me. Here's how you do it:

 

First, add a file called copyFile.lua to your project with the following code:

--------------------------------------------------------------------------------
-- CHECK IF A FILE EXISTS
--------------------------------------------------------------------------------
local function fileExists(filePath)
    local exists = false
    local fileHandle = io.open( filePath, "r" )
    if (fileHandle) then -- nil if no file found
        exists = true
        io.close(fileHandle)
    end
    return(exists)
end

--------------------------------------------------------------------------------
-- COPY A FILE
--------------------------------------------------------------------------------
local function copyFile(source, destination)
    if not fileExists(source) then return nil end

    local sourceFile = io.open( source, "r" )
    local destinationFile = io.open( destination, "w" )

    local fileData = sourceFile:read( "*a" )
    if not fileData then
        print( "read error!" )
        return false
    else
        if not destinationFile:write( fileData ) then
            print( "write error!" )
            return false
        end
    end

    sourceFile:close()
    destinationFile:close()
    return true
end

return copyFile

Then require that module into your project like so:

local copyFile = require("copyFile")

Then copy your image file (with the fake ".txt" file extension) to your documents directory like so, giving the copy the right file extension:

copyFile(system.pathForFile("tweetImage.txt", system.ResourceDirectory), system.pathForFile("tweetImage.jpg", system.DocumentsDirectory))

Now that you've made copy in your documents directory, which your Android device can see and work with, you can tweet that image out like so:

twitter.tweet("Wow this was a lot of work to tweet this image!", "tweetImage.jpg", callbackListener)

I hope that all makes sense. I know it's a lot of work, but keep in mind that if you are tweeting images that are already in your system.DocumentsDirectory then none of this stuff needs to happen - you can go ahead and tweet those images easy-peasy. Thanks for your patience, and enjoy! :)



[TOPIC: post.html]
#17

schroederapps

[GLOBAL: userInfoPane.html]
schroederapps
  • Contributor

  • 407 posts
  • Corona SDK

Hi @Anil, just confirming that the latest version of the Plugin is live and should automatically download next time you fire up the Simulator. And I just posted an image successfully from the Windows simulator using the actual live plugin, so you should be good to go! Thanks again for your patience.



[TOPIC: post.html]
#18

anil8

[GLOBAL: userInfoPane.html]
anil8
  • Contributor

  • 124 posts
  • Corona SDK

Thanks for the hard work - we all appreciate it.

 

Will test it out tomorrow and let you know if all is good.




[topic_controls]
[/topic_controls]