Dropbox Rest Api Sample Code In Code Exchange

Hey Folks,

If you’ve been wanting to add Dropbox to your app, I am donating some code that will make the job much easier.  You can find it here: http://developer.coronalabs.com/code/dropbox-rest-sample-code

 

This is sample code that will allow you to download text files to your app in Corona or upload text files from your app. Since this code is based on the REST API, it’s possible it could be use for other web services besides Dropbox- however, it will only work with other web services that allow the use of PLAINTEXT.

 

This sample code is based on the advice provided here:
https://www.dropbox.com/developers/blog/20

 

This code will allow you to get request token, authorize via webPopup, get and store access token and get or put text files. You need to register with Dropbox as a developer, create an app and add your consumer key and consumer secret to the dropbox.lua file.

 

To use the file as is, you need to rename it to main.lua and then update a few of the variables at the top including consumer key, consumer secret and myFile. I may turn the file into a module, but if you would like to do it and post it here in the code exchange, that would be great too!

 

If you’re developing for Android on Windows, you will need to run the app from a device (webpopups don’t work in Windows simulator). Also, you will need to have the device attached by USB and use the terminal to monitor results. You can use the following command to see what Corona is up to on the device:
adb logcat Corona:V *:S

Let me know if you have questions.

Thanks for your hard work on this. A module would be greatly appreciated if you have the time. I was struggling to make sense of your last version of this since I didn’t want the buttons. I’ll check out the current version and see if it reads easier!

I may turn the file into a module, but if you would like to do it and post it here in the code exchange, that would be great too!

Hey Wyldkard,

Finally, someone expressed interest in this.  I thought I was the only one who needed this functionality.

This version still has buttons, but I believe it is much more straightforward.  Please let me know what you think. 

I’ve been itching for a proper Dropbox implementation for months, actually, but haven’t had much success. A huge shame, because I was able to mock up an app idea in Pythonista for iOS quite quickly due to Python’s extensive libraries (which includes a Dropbox library). (I don’t even know Python!)

My goal is to read in a bunch of text files, process them, and then export new files in a particular hierarchy. I tinkered with your old version, as well as the version someone wrote for Codea, but it didn’t work for me. Will look at your latest version tonight!

Tried to run your example, but getting a forbidden error…

Need more info.  How far did you get before an error occurred?   

Are you using adb logcat Corona:V *:S to monitor the activity on your device while the code is running?

Did you create a Dropbox app at https://www.dropbox.com/developers and insert your consumer key and consumer secret into the main.lua file at the correct location?   Did you rename my dropbox.lua file to main.lua?

If you can provide more info, I can help you debug.

I’m not developing for Android, so the adb logcat command shouldn’t apply, right? I see print statements and status information in terminal.

The Dropbox app was created, and I can connect to it fine using a prototype I built in Pythonista, so I know the consumer key/secret is set correctly. When I tap the Get Info button in the simulator, I get:

2013-04-17 13:19:08.971 Corona Simulator[50810:707] pre get info request 2013-04-17 13:19:08.972 Corona Simulator[50810:707] post get info request 2013-04-17 13:19:08.972 Corona Simulator[50810:707] rawGetRequest result: userdata: 0x10dd14258 2013-04-17 13:19:10.431 Corona Simulator[50810:707] rawGetListener RESPONSE: 403 {"error": "Forbidden"}

Does this only work on-device?

Looks like I still had some older files from a previous version of your bundle in my app’s documents folders, so I removed them, Upon restarting, I press the Connect button and get a response in the terminal. I also get a response in-app via a web pop-up asking if I want to connect to Dropbox. I go ahead, and then press the Get Info button, and again get the error from my last post.

Okay, I got it now. I needed to un-comment this line:

local url = "https://api.dropbox.com/1/account/info"

And then comment out the line two below it. Now the app can retrieve my account data and display it accordingly. Again, nice work on putting this together, michael714!

Glad you got it to work.   Please let me know if there are any changes you think I should make in the file or if there are any comments that I should add.

Thanks for being my alpha tester!

Is there a reason you commented that line out above to get the account info?

I guess I was confused with the getSomething function, because it grabs account info _or_ a file, depending on which line is commented out. You could create a getAccountInfo and getFile function, or just have it grab account info if no argument is passed in, else have the argument be the file name to grab. 

As far as comments to the readme go, it may be worth stating exactly which lines of code need to be changed/appended, as well as where to put the file to “put” on Dropbox. It wasn’t immediately obvious to me that the file needed to go into the Documents folder, instead of in the same folder as my app’s resources. Newbie mistake, but not entirely obvious to me. I think you included a .csv file in an earlier distro - I just used one of the included graphics resources.

I think converting this into a module would be the way to go. Let me know if you don’t plan on it, and maybe I’ll see what I can put together. Not sure how much time I’ll have to work on this this week, but now that the basic Dropbox connection works, I hope to tinker some more and see what else needs to be added.

As a side-note, with the REST API, is the file saved locally automatically when you grab it with getSomething? Does that mean the file needs to be explicitly deleted locally once you’re done? My goal is to read in a bunch of files, but I don’t want them to be saved locally.

Thanks for the response.  I’ll look at this a bit more this weekend.

Thanks for your hard work on this. A module would be greatly appreciated if you have the time. I was struggling to make sense of your last version of this since I didn’t want the buttons. I’ll check out the current version and see if it reads easier!

I may turn the file into a module, but if you would like to do it and post it here in the code exchange, that would be great too!

Hey Wyldkard,

Finally, someone expressed interest in this.  I thought I was the only one who needed this functionality.

This version still has buttons, but I believe it is much more straightforward.  Please let me know what you think. 

I’ve been itching for a proper Dropbox implementation for months, actually, but haven’t had much success. A huge shame, because I was able to mock up an app idea in Pythonista for iOS quite quickly due to Python’s extensive libraries (which includes a Dropbox library). (I don’t even know Python!)

My goal is to read in a bunch of text files, process them, and then export new files in a particular hierarchy. I tinkered with your old version, as well as the version someone wrote for Codea, but it didn’t work for me. Will look at your latest version tonight!

I played around with this a bit more yesterday and began converting your code into a module. Currently, I have my app connecting to Dropbox, opening up the permissions popup, and reading the contents of a file. I still need to hammer out which functions and such should be exposed, but will hopefully have something a little more concrete this week.

michael 714, if you’re _not_ planning to post this as a module and would like me to post my version, just let me know.

Yes, please, go for it!  I added you as a collaborator on the github repository or you could fork it.  Or, let me know if you would prefer to use another type of revision control system.  

Thanks!

Tried to run your example, but getting a forbidden error…

Need more info.  How far did you get before an error occurred?   

Are you using adb logcat Corona:V *:S to monitor the activity on your device while the code is running?

Did you create a Dropbox app at https://www.dropbox.com/developers and insert your consumer key and consumer secret into the main.lua file at the correct location?   Did you rename my dropbox.lua file to main.lua?

If you can provide more info, I can help you debug.

I’m not developing for Android, so the adb logcat command shouldn’t apply, right? I see print statements and status information in terminal.

The Dropbox app was created, and I can connect to it fine using a prototype I built in Pythonista, so I know the consumer key/secret is set correctly. When I tap the Get Info button in the simulator, I get:

2013-04-17 13:19:08.971 Corona Simulator[50810:707] pre get info request 2013-04-17 13:19:08.972 Corona Simulator[50810:707] post get info request 2013-04-17 13:19:08.972 Corona Simulator[50810:707] rawGetRequest result: userdata: 0x10dd14258 2013-04-17 13:19:10.431 Corona Simulator[50810:707] rawGetListener RESPONSE: 403 {"error": "Forbidden"}

Does this only work on-device?