Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Dropbox Rest Api Sample Code In Code Exchange
Started by michael714 Apr 01 2013 06:29 PM

31 replies to this topic
dropbox rest oauth sample network
[TOPIC CONTROLS]
Page 1 of 2 1 2
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

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.



[TOPIC: post.html]
#2

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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!



[TOPIC: post.html]
#3

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

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. 



[TOPIC: post.html]
#4

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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!



[TOPIC: post.html]
#5

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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



[TOPIC: post.html]
#6

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

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.



[TOPIC: post.html]
#7

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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?



[TOPIC: post.html]
#8

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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.



[TOPIC: post.html]
#9

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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!



[TOPIC: post.html]
#10

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

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!



[TOPIC: post.html]
#11

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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.



[TOPIC: post.html]
#12

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

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



[TOPIC: post.html]
#13

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

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.



[TOPIC: post.html]
#14

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

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!



[TOPIC: post.html]
#15

gslender

[GLOBAL: userInfoPane.html]
gslender
  • Pro
  • PipPipPipPipPipPip
  • 53 posts
  • Jedi

So, how we going?

 

I'm now looking at how to add DropBox file reading, directory viewing and putting a file up - all this support is fairly basic, but extensive in coding and testing... so would love to jump on the back of someone else's work to help me along, and of course anything I add or improve, I'd happily post back and contribute.

 

G



[TOPIC: post.html]
#16

michael714

[GLOBAL: userInfoPane.html]
michael714
  • Pro
  • PipPipPipPipPipPip
  • 153 posts
  • Jedi

I don't know that there's been any progress on the modulization, but the code that's there will get you started.  You would need to add functionality if you want to upload and download binary files or view the directory.   Give it a  look.



[TOPIC: post.html]
#17

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

Hey guys - sorry for the silence, but I haven't had much time to work on this of late. I'm not well-versed with Github, so for now I set up a quick Gist for my code-to-date. It's still in a pretty rough form and needs a lot of polish, but the core functionality works, thanks to the work pioneered by Michael. https://gist.github.com/WyldKard/5589284

 

If you have questions about how to utilize the module, drop them here.



[TOPIC: post.html]
#18

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

FWIW, I just updated the gist so the file upload function works properly.



[TOPIC: post.html]
#19

rxmarccall

[GLOBAL: userInfoPane.html]
rxmarccall
  • Starter
  • PipPipPipPipPipPip
  • 751 posts
  • Jedi

@syldkard,

Thanks for posting your code.  Does this only allow for accessing text based files? Or can it "put/get" images, etc?



[TOPIC: post.html]
#20

martin.kilkie

[GLOBAL: userInfoPane.html]
martin.kilkie
  • Starter
  • PipPipPipPipPipPip
  • 75 posts
  • Jedi

Hi Michael and wildcard. I am keen to put some dropbox functionality into my corona apps and have been reading through your conversations as Michael then wyldkard developed the idea.

I have also been to the dropbox developer site and got me a token and secret and then got the latest code that wyldkard had put in git.

 

I have been trying to read through the code and I am finding it a bit dense. I suspect that once I understand how it all works then the code will be self explanatory, but at the moment I am finding it hard going. Perhaps a bit more documentation comments in the file??

 

What is the connection with google.com?

Also in wyldkard's version I cannot see which functions are called and which are just internal calls. perhaps a short few sentence explanation of how you would use the lua?

 

for example. I want the user to access their own dropbox from within my app. I have a token and a secret. What do I need to do next in a few lines of code calling on your code?



[TOPIC: post.html]
#21

martin.kilkie

[GLOBAL: userInfoPane.html]
martin.kilkie
  • Starter
  • PipPipPipPipPipPip
  • 75 posts
  • Jedi

Hi Both,

 

Ive been playing a bit more and managed (through trial and error) to get a combination of the lines that were commented out uncommented and the app connects to dropbox, asks for my login then gives me the choice to give permission, then nothing. When I go to my dropbox account there has been some access and a file is there but not the named one.

 

Is there a version of the file with the correct lines commented that just works?  then I can see where I am going wrong. At the moment it is like blind mans buff.

 

Martin



[TOPIC: post.html]
#22

wyldkard

[GLOBAL: userInfoPane.html]
wyldkard
  • Starter
  • PipPipPip
  • 30 posts
  • Advanced Member

Hey guys, sorry for taking so long to respond. I haven't had a chance to do any coding in the last couple months, so neglected this forum. Have you had any more luck getting this to work? FWIW, the gist I linked to above is the same module I use in my app Str, which uses the module to save files to Dropbox.

 

rxmarccall: The module should let you put/get files of any type.

 

martin: I will make an effort to document the steps you need to take to make basic calls to Dropbox once you have your key/secret, and then add them to the gist for easy reference. Once I get that added, I'll post about it here.

 

In regards to google.com, the webURL can be anything. Here, google.com is just a placeholder, but feel free to change it to whatever; it's used here as a callback address, but it doesn't respond to anything, so it doesn't matter what you use,

 

This was actually my first Lua module, so I apologize if I broke expected standards. Any functions with "local" in front of them are internal. The other functions are external and should be called from your main code - basically all functions from M.connect() onward.

 

I don't have my code in front of me, but from recollection, from your main app be sure to import the Dropbox.lua module. Call the connect() function and pass it your key and secret for your respective Dropbox app, as well as "root" or "sandbox" depending on what level of Dropbox access you want to give your app. The first time you do this, your app will pop up a web view requesting you to log in to your Dropbox account.

 

Once that's done, you can call the other functions to access your Dropbox. Best way to verify things are working is to call the getAccount() function which should print your account info to the console. If you're getting the correct response here, you can go ahead and use the get/put functions but you have to specify a listener in your main code so your app does something with the response. Let me know if you need more help with that and I'll post an example.



[TOPIC: post.html]
#23

martin.kilkie

[GLOBAL: userInfoPane.html]
martin.kilkie
  • Starter
  • PipPipPipPipPipPip
  • 75 posts
  • Jedi

Hi wyldkard, still playing. I have almost got it sorted. I have my secret etc and have a tester app in development to sort out the various routines I need. I am stuck at the moment with getting a directory from the dropbox account. have you done this?

 

Martin



[TOPIC: post.html]
#24

xnailbender

[GLOBAL: userInfoPane.html]
xnailbender
  • Pro
  • PipPipPipPipPipPip
  • 256 posts
  • Jedi

First off, thanks guys for your work with the Dropbox module.  What a huge help it's been for me.  2 THUMBS UP!

 

@Martin, I've been working with the module for a week now and can get and parse the Dropbox meta call.

 

Not sure where you are at integrating it, so I'll just post a hard coded example, which I usually do until I get it working and then add in the functions that actually supply the needed params/ variables.

 

First, I manually created a folder in my Dropbox  "Public" folder called,  "MyTestFolder"

 

in my URL, I replaced "sandbox" with "dropbox" to get it to work.

 

I had to remove the lua tags around my code below as they are formatiing my code in an unusable long string....hmmmmm

 

----------------------------------------

 -- Gets metadata for the account.
function M.getMeta( path, listener )
    --local url = "https://api.dropbox.com/1/metadata/sandbox/" .. path .. "?" .. authString
       

--  I've added the M.connect function to all my calls also, to make sure the user is signed in before trying to access the Dropbox account.

 

M.connect(consumer_key, consumer_secret, "sandbox")

 

--notice the "/" following the folder name I want to browse
    
        local FilePath = "Pulblic/MyTestFolder/"

 

local Listener = M.downloadDropboxMetaListener


    local url = "https://api.dropbox.com/1/metadata/dropbox/" .. FilePath .. "?" .. authString
    network.request( url, "GET", Listener )
end

-------------------------------

 

OK, now you made the request for the meta for the specified Dropbox folder.

You will get a response from the request, the print will be called at the top of the dropboxListener()

 

Paste the listener code below into the module to read the response.

 

---------------------------

 

local JsonResponse --I declare it at the top of the module so I can use it elsewhere

 

function M.downloadDropboxMetaListener(event)
    
    if ( event.isError ) then
        print( "Network error!")
    else
        print("M.downloadDropboxMetaListener(event) has been called")
        print ( "RESPONSE: " .. event.response )
        
        JsonResponse = json.decode(event.response)
        print("JsonResponse == ", JsonResponse)
        print("JsonResponse.error == ", JsonResponse.error)
        
        if JsonResponse.error == "Access token not found." then
            print("<<<<<<<<<<<<<<  JsonResponse.error == 'Access token not found.")
            native.showAlert("Login to Dropbox!"], "First Login to Dropbox then select the action again.", {"OK"}, noticeLoginToDropboxAndTryAgainStep2)   
            return
        end  
        
        
        if #JsonResponse.contents < 1 then
            print("<<<<<<<<< There are NO Saved Registers!")
            
            native.showAlert("Download Error: Empty Dropbox Folder!", "The mTotal Admin must Upload the requested files.", {"OK"}, noticeEmptyDropboxFolderStep2)
            
        else       
            
            for i = 1, #JsonResponse.contents do
                print("JsonResponse.contents["..i.."].path == ", JsonResponse.contents[i].path)
                
            end
            

           --call your function to process the response and load it into a scrollView
            --loadDropBoxRScrollViewMenu()
        end
    end
    
end

-------------------------------------

 

I hope I don't have a typo in there somewhere, it should work.  I had to remove a bunch of code out of the listener that I use in my app, but this should get you headed in the right direction.

 

Hope this helps.

 

Nail



[TOPIC: post.html]
#25

hmob

[GLOBAL: userInfoPane.html]
hmob
  • Pro
  • PipPip
  • 11 posts
  • Member

Hey guys, new to the thread, looking to integrate to dropbox (pretty slick!), I was wondering if there is a 'good' or latest version of the module you guys worked on? seems like a bunch of changes took place over many months..

 

Many thanks!!

 

hools




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]



Also tagged with one or more of these keywords: dropbox, rest, oauth, sample, network