Dropbox Rest Api Sample Code In Code Exchange

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

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

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?

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

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.

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

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

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

@hmob,

The code in exchange worked for me with the modifications I made above.  I don’t know of an updated version.

You’ll probably be making the Dropbox URL  dynamically, so make sure to take out any spaces in the URL or it wont’ work.  If your app will allow users to create and label their files to be uploaded,/downloaded,  sub out the spaces in the label strings.

It’s cool how Dropbox will dynamically create  folders in the parent if they don’t exist.

GL

Nail

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

@hmob,

The code in exchange worked for me with the modifications I made above.  I don’t know of an updated version.

You’ll probably be making the Dropbox URL  dynamically, so make sure to take out any spaces in the URL or it wont’ work.  If your app will allow users to create and label their files to be uploaded,/downloaded,  sub out the spaces in the label strings.

It’s cool how Dropbox will dynamically create  folders in the parent if they don’t exist.

GL

Nail

Hey Folks,

It’s been a while since I looked at the Dropbox code and I like what Wyldcard did.  However, the module implementation seemed like it was incomplete.  So, I made a few changes and I am sharing them here with you: https://www.dropbox.com/sh/evttsokenvqp7lv/AADr47rIPBq6-HR3Akm7YOyra

I’ve included a DropBoxModule file and a download.lua file which demonstrates how you use the DropboxModule.lua file.   Hope this helps!

I also posted this version in the new Code Exchange.

Thanks VERY much for putting this together. I’m using this as a base to build out other parts of the api, but I’m running into trouble with the fileops/copy command. It seems to be ignoring the values in the params table, whether I pass them in at the top level of the table, as a set of headers, or as a body. Do you happen to know the correct way to pass the params table to that command?

Here’s what I’ve got:

 

[lua]
function M.copyWithCopyRef( path, fileName, listener, copy_ref )
local url = “https://api.dropbox.com/1/fileops/copy” … “?” … authString
local params = {}
params[“root”] = “sandbox”
params[“from_copy_ref”] = copy_ref    --params[“from_path”] = “/myFile.txt” – same error
params[“to_path”] = path…fileName
network.request( url, “POST”, listener, params )
end

[/lua]

 

API Reference: https://www.dropbox.com/developers/core/docs#fileops-copy

error from dropbox:
{“error”: “Must send either a from_path or a from_copy_ref”}

Also, when I’m done converting more of the API over I’ll be happy to send you my module to include in the code exchange if you’d like.

 

Thanks again!

I have a couple of suggestions: 

  1. It is possible you could pass those parameters to Dropbox by appending them to the URL you are sending as opposed to submitting them as headers.   

  2. If you want to pass them as headers, you should use this form:

local params = {}

local headers = {}

headers[“Content-Type”] = “text/plain”

headers[“Authorization”] = "OAuth "…rawdata

params.headers = headers

local result = network.request( url, “POST”, rawPostListener, params)

return result

Gotcha - passing them as URL parameters worked perfectly - thanks!

Hey Folks,

It’s been a while since I looked at the Dropbox code and I like what Wyldcard did.  However, the module implementation seemed like it was incomplete.  So, I made a few changes and I am sharing them here with you: https://www.dropbox.com/sh/evttsokenvqp7lv/AADr47rIPBq6-HR3Akm7YOyra

I’ve included a DropBoxModule file and a download.lua file which demonstrates how you use the DropboxModule.lua file.   Hope this helps!

I also posted this version in the new Code Exchange.

Thanks VERY much for putting this together. I’m using this as a base to build out other parts of the api, but I’m running into trouble with the fileops/copy command. It seems to be ignoring the values in the params table, whether I pass them in at the top level of the table, as a set of headers, or as a body. Do you happen to know the correct way to pass the params table to that command?

Here’s what I’ve got:

 

[lua]
function M.copyWithCopyRef( path, fileName, listener, copy_ref )
local url = “https://api.dropbox.com/1/fileops/copy” … “?” … authString
local params = {}
params[“root”] = “sandbox”
params[“from_copy_ref”] = copy_ref    --params[“from_path”] = “/myFile.txt” – same error
params[“to_path”] = path…fileName
network.request( url, “POST”, listener, params )
end

[/lua]

 

API Reference: https://www.dropbox.com/developers/core/docs#fileops-copy

error from dropbox:
{“error”: “Must send either a from_path or a from_copy_ref”}

Also, when I’m done converting more of the API over I’ll be happy to send you my module to include in the code exchange if you’d like.

 

Thanks again!

I have a couple of suggestions: 

  1. It is possible you could pass those parameters to Dropbox by appending them to the URL you are sending as opposed to submitting them as headers.   

  2. If you want to pass them as headers, you should use this form:

local params = {}

local headers = {}

headers[“Content-Type”] = “text/plain”

headers[“Authorization”] = "OAuth "…rawdata

params.headers = headers

local result = network.request( url, “POST”, rawPostListener, params)

return result