Best answer

How do I copy a whole Dropbox structure from one place to another

  • 21 October 2022
  • 7 replies
  • 588 views

Userlevel 1
Badge

It's my first time on these forums, and I'm having a minor issue with Dropbox. I was wondering if someone could help. Someone alluded to a solution on the forums about a year ago here:

https://community.zapier.com/dropbox-q-a-69/create-duplicates-of-existing-dropbox-folders-8731?postid=41639#post41639

In short, I'm trying to copy over a folder structure from one place to another within Dropbox. There seems to be a solution by linking a Webhook to the Dropbox API, and I'm a bit out of my depth when setting up webhooks and talking to an API.

Any chance you can help with step-by-step instructions for setting this up

icon

Best answer by sillywonka 22 October 2022, 18:14

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

7 replies

Userlevel 1
Badge

@Troy Tessalone Any ideas?

Userlevel 7
Badge +14

Hi @sillywonka 

Good question.

The Dropbox API has a copy endpoint: https://www.dropbox.com/developers/documentation/http/documentation#files-copy

App APIs can be used in Zaps with the Webhooks app as an action step: https://zapier.com/apps/webhook/help

I have not done this specific Zap configuration yet.

 

Userlevel 1
Badge

Ok, so I got this to work! Yay! @Troy Tessalone Thanks for your help.

 

For others looking to do this who aren’t as code-savvy. I mean, that’s why we’re here, no?: 

 

On the Zapier side:

I’m using a Webhook Zap with a POST Event, and the configuration is pasted at the end.

You copy over the URL from the Dropbox API documentation. Note that the “from_path” and “to_path” data field inputs have to be added with the plus box, and the “from_path” must be accessible in Dropbox through Zapier already (i.e., if you can’t find the folder using a normal Dropbox Zap, this won’t work).

You can point to a file or a folder. If it’s a folder, the whole folder structure will come through with the files inside that folder. Also, the “to_path” needs to end at the name of the folder you want to create; for example, to_path would be set to “/to/my/path/new_name,” where the new name is the name of the folder you want the new contents to be dropped into.

 

Some things to note:

Copying the paths from, say, a “windows explorer bar” needs to be reformatted to forward slashes “/” from backslashes “\” so:

\to\my\path\folder_to_copy 

Becomes

/to/my/path/folder_to_copy

 

On the Dropbox side:

I’ve had to create a Dropbox App. I’m not sure all users can create apps. We have a paid business account.

https://www.dropbox.com/developers/

In the App Console, we’ve created an app and granted it OAuth 2.0 access, setting it to “Allow Public Clients” in the “Settings” tab. Then in the “Permissions” tab, we’ve given the app “Files and folders” read and write access and checked off the “openid” checkbox. Note that this configuration means you won’t be able to give the app permission for “Team Scopes.” Not sure if that matters to anyone else. It seemed to be necessary that this box be checked for this to work. (Maybe?)

 

Finally, once that’s done and you’re logged in, you can click on the Dropbox API documentation that Troy posted and click on the “<get access token>” within the example. It’s a bit hard to see, but it's a blue link, and if you’re logged in, it will generate the access token for you. Copy that long ass string, including the “Bearer ” (with the space) at the front.

https://www.dropbox.com/developers/documentation/http/documentation#files-copy

 

Back in the Zap

In the Headers section, create a new Header and label it “Authorization” (no quotes), and paste the copied value into the value side box on the right.

Here are other things to note here before hitting the test button.

Payload Type: JSON.

Wrap Request In Array: set this to No since Dropbox is not expecting a list.

File: None are needed for this.

Unflatten: Set this to Yes.

Basic Auth: Leave this field blank, as this will cause the webhook to try to log in with standard credentials, but the post requires an authentication header. Having both gives an error.

 

Lastly, fire up a folder window and navigate to the folder where the stuff will be copied, and hit the test button. You should see it pop up after a few seconds. Adjust the paths if it's not what you expected.

 

 

Good luck, everyone! Happy Zapping!

Userlevel 7
Badge +9

Hey friends, this is wonderful! Super glad y’all were able to work this one out and we extra appreciate you coming back to this post to share your solution with the community! 🙌

@Troy Tessalone @sillywonka 
Have any of you received this error when creating this zap? 

The app returned "Error in call to API function "files/copy:2": The given OAuth 2 access token is malformed.".

 

Thank you :) 

Userlevel 7
Badge +14

@sydneylynn 

Double-check the access token is accurate.

Make sure there are no extra spaces characters before/after.

Try going thru the steps again to configure.

Userlevel 1
Badge

Ok. So quick update. 

Because I ran into Token issues as well and I have officially solved it.

 

The access token I suggested you get from clicking on the blue text in the last comment always starts with an "sl." That token is apparently TEMPORARY or "short-lived." So you need a way to refresh that token every time you run the trigger. 

So we need to add a new Webhook Zap BEFORE the inital Zap i talked about earlier. Here’s what mine looks like:

 

Set it up as follows:

In Dropbox

 

Bear with me. You officially need to get Zapier to provide you a "refresh token" which is what they call a "long-lived" token that you can use consistently to create new "short-lived" tokens (the ones that start with sl which get passed to the next zap). In order to get the refresh token, you need an access code. How silly.

 

To get an Access Code from Dropbox

 

Access Codes are generated by sorta "frankensteining" this URL. Note that this is the "offline version" of the URL. That's the one you want because it returns the refresh token:

https://www.dropbox.com/oauth2/authorize?client_id=<APP_KEY>&token_access_type=offline&response_type=code

 

Where "<APP KEY>" is replaced by the value in the App Console in Dropbox, you'll need to copy and paste in place of <APP_KEY>.

 

Once you've created the URL, copy it into a browser, press enter, and follow the prompts. It should bring you to a window that looks like this:

Copy the long code (usually 43 characters long) and save it somewhere for now. It's important to note that Access Codes are also temporary. More silliness.

 

Back in Zapier

 

Again, this part is the tricky bit. You need to run the new Webhook zap in one configuration to have it return the refresh token code. Then you need to change the configuration of the same Webhook zap to use it to continually generate new short-lived tokens, which you will pass on to the next zap every time.

 

Part 1: The Temporary set up.

 

The goal here is to generate a response from the zap when you test it that will give you a "refresh token" key.

 

You want to select POST from the dropdown. I tried custom, and it totally breaks.

 

In the URL Box, you want to copy this in:

https://api.dropboxapi.com/oauth2/token

Payload type: form

Data:

In the key/value pairs in the Data section you want 4 variables:

  • code
  • grant_type
  • client_id
  • client_secret
  1. The code should be set to the code you just got from Dropbox (the one got by running the frenken-URL).
  2. grant_type should be set to the words "autorization_code" all lowercase, no quotes.
  3. client_id is set to your App Key in Dropbox, the one you copied before to make the first URL
  4. client_secret should be the secret key found in the App Console in Dropbox. Never share that one.

Everything else should be left at the default settings:

Wrap Request in Array: No
File: None
Unflatten: Yes
Basic Auth: Leave Blank

 

For the Header, it's crucial to create one key-value pair otherwise, it throws an error:

Content-Type =  application/x-www-form-urlencoded

 

 

Once you've filled out the zap, now is the real tricky bit. You hit test button and you should recieve a response from Dropbox with 7 key/value pairs. One of those pairs is the "refresh_token". You can see it in Blue below:

 

Part 2: The Permanent Setup

 

COPY the refresh_token you get from the test. This is the "long-lived" token you can now use to generate new "short-lived" tokens for the actual copy webhook zap.

 

Now go back into the "Set up Action" for the same Webhook you just tested and reconfigure it because a refresh token request is slightly different:

 

  • remove the "code" key/value pair. It's no longer necessary.
  • set grant_type to refresh_token
  • Create a new key value pair and set the key (smaller box) to "refresh_token" (no quotes) and set the value of that key (larger box) to the code you copied (the one you got from running the test).

Everything else stays the same. Now re-run the test.

 

You should have a response from Dropbox with only three key value pairs and the first one is the short-lived access_token which you can use in the next webhook. You can see mine below. This one has already expired.

 

 

Where to pass it in the next Zap

 

So it should go after the word "Bearer " (no quotes with a space) in the Header section of the next zap.

 

And that's it! The overall “zap flow” should now create a new short-lived token every time.

 

Big props to Codemzy for writing this blog where I was able to sort of piece it together after reading a clearer explanation of how these work:

 

https://www.codemzy.com/blog/dropbox-long-lived-access-refresh-token#:~:text=Luckily%2C%20linking%20it%20to%20your,for%204%20hours%2C%20before%20expiring

 

and to my Dev friend Brian for struggling through teaching me about Webhooks.

 

Ugh. All that just to copy a few folders over from one place to another at scale.a C’mon Zapier. Just add “Copy Mutiple Folders” to the Dropbox integration. How silly.

 

Good luck y’all!