Dropbox and routing data

I’m new to this group and I just received my notecard. I’m curious is sending data to Dropbox via routine possible? Has anyone done it yet?

Rabdy

1 Like

Rabdy,

Welcome! I’m glad you’re curious and have posed such an interesting question. I just got it working as a proof of concept:

  1. First, decide on a file name for the files which will be created in Dropbox for each Note you create on your notecard. For example test.txt will cause each file to be named test.txt (1) test.txt (2) etc. You’ll need this in step 23 below.
  2. Go to Dropbox Apps
  3. Click “Create App”
  4. Select “Scoped access”
  5. Select “App folder”
  6. I named my app carl-test-app you’ll need this later
  7. (optional) I chose my ‘Personal’ dropbox because I also have a work dropbox so had to pick one
  8. Click “Create App”
  9. Click the Permissions tab
  10. Tick the box for “files.content.write”
  11. Click [Submit] button
  12. Click back to the ‘Settings’ tab.
  13. Click the “Generate” button to create an access token in the OAuth 2 section.
  14. Copy the token for later. It looks like sl.BNHbRdx3lg…
  15. Go to your notehub project
  16. Click ‘Routes’ in the left side menu
  17. Click ‘Create Route’
  18. Select the first type “General HTTP/HTTPS Request/Response”
  19. Name your route anything you like.
  20. For the URL put in https://content.dropboxapi.com/2/files/upload
  21. For HTTP Headers, select “Additional Headers”
  22. Add a header named Authorization with value Bearer sl.BNHbRdx3lg... with your whole long token from step 13 above.
  23. Add a header named Content-Type with value application/octet-stream
  24. Add a header named Dropbox-API-Arg with value {"autorename":true,"mode":"add","mute":false,"path":"/test.txt","strict_conflict":false}, using the filename you decided on at step 0 above. If you’re going to be making a lot of notes, set mute to false on this header so Dropbox won’t keep popping up notifications on all your devices when each file is created.
  25. (optional) filter down the notefiles or transform them using those features of the ‘create route’ page.
  26. Click the ‘Create new Route’ button to save your work and activate the route.

Done!

Now, when you use the notecard to create notes and sync them to the cloud, Notehub will automatically route each note to Dropbox to the /Apps/carl-test-app folder as a new txt file.

image

Here’s what my route looks like:

Resources:

Again, welcome to the community. Please let us know if I missed any steps while I was writing this up or if you have any more questions.

-Carl

2 Likes

Thanks for this!

I tried setting up this morning and I must be doing something wrong as I appear to be getting an authorization err? below is the response, I have edited the token value


{“attn”:true,“status”:“400”,“text”:“Error in call to API function "files/upload": Invalid authorization value in HTTP header "Authorization": "Bearer sl. v4Xhjq9PX-OA7hm9aOwM7z_0pe1AcKxPQ". Expecting "Bearer ".”}


Looks like you might have an extra space after the sl. in your token on the notehub route configuration. Let me know if that fixes it.

Also, I realized this morning, when my route stopped working, that sl. means short-lived, because the token generated by the Generate button doesn’t last forever. To get a token that doesn’t expire so quickly in a production environment you’ll want to read up on Dropbox OAuth Guide - Dropbox

Thanks for that, I now have another issue

{“attn”:true,“status”:“401”,“text”:“{"error_summary": "missing_scope/…", "error": {".tag": "missing_scope", "required_scope": "files.content.write"}}”}

I have defintitely enabled files.content.write in the dropbox permissions. I even unchecked, saved and rechecked and I get the same results?

Ok, disregard the last post. I have no idea what the problem was. Everything as far as I could tell was set up correctly in dropbox. To resolve, I deleted and re-did the dropbox setup. The only thing I did differently is generate the token after I had the permissions set. Anyway, I have it working, thanks for your help on this, I am sure it would have taken me much longer slogging away without your input

That’s great news. I’ll update the order of operations in my original post for the benefit of the next person. It’s so good to help you build whatever it is you’re building :smiley:

By the way, now that you got it working, here’s a cool way to filter and transform what data gets stored in the file in Dropbox. Add a JSONata Expression. On the route-edit page I added this expression {"body":body, "time": $fromMillis(when * 1000)}


so now my txt files are a lot simpler.
image

JSONata expressions can even let you use more advanced logic to decide which notes should be routed to Dropbox.

Resources:

Good stuff, I will look into this tomorrow at some point. I am still a little thrown that the order of generating the token is important. Oh well, I will just move on. So far things seem to work as documented, although for some reason I had problems with curl requests to the notehub, but postman worked fine, likely just me though.

FWI, I am looking at doing some automation for my cottage, control the furnace ,monitor temperature. Our internet signal is poor up north and this is what I have been looking for as my needs for this is not necessary low power, but certainly low data transfer, so I am exited. The only hitch that I will not be able to test until the weekend at the earliest is cell signal. We get a weak signal, but consistent for phone and data so I am hoping that this device with the low data needs is perfect. Thanks for all of your help so far.

randy

Thanks again for all of the help. I took a brief look at the Oauth on dropbox. I am a bit confused on their implementation, it looks like you need to get a key through an http auth process and then redirect with that key. Does your routing handle that? Have you had a look at this process for dropbox? I am going to do some more research on the dropbox side to better understand the flow, but I thought I would reach out and ask you if you have had any experience with dropbox an Oauth.

randy

I skimmed the Dropbox OAuth guide but I didn’t have time to understand all of it. My best guess for your path forward would be to understand Implementing Code Flow which will let you get a long-lived authorization code and to turn that into an access token (like the one generated by the ‘Generate’ button). That access token will also be short lived (dropbox just gave me one that expires in 4 hours) but then Using Refresh Tokens will help your software generate new access tokens using the long-lived authorization code.

Every time you get a new access token, you could have your program update the access token in the Notehub Route using the Notehub API Route API - Notehub API Reference - Blues Wireless Developers

Unfortunately Notehub isn’t currently capable of acting like an OAuth client (to automate the steps above). I’ll float that as a feature request with the Notehub team.

Yes, I was thinking that maybe the route to go was the notehub api.

thx.

In the spirit of giving back, I have copied below the process to allow a programatic method of retrieving short term tokens. Thanks for your help getting started!

  1. This first step is interactive and requires the dropbox owner to authorize access. This step will provide an “Authorization_CODE” which can be copied and used in subsequent steps

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

  1. Using the authorization code from above, execute this step which will provide both a short term token and a refresh token. The short term token can be used, but it is the refresh token that is important for generating short term tokens in the future

curl https://api.dropbox.com/oauth2/token -d code=<AUTHORIZATION_CODE> -d grant_type=authorization_code -u <APP_KEY>:<APP_SECRET>

  1. This final step uses the refresh token from step 2 to generate a new short term token. This appears to be the only step that needs to be run each time to get a short term token.

curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=<REFRESH_TOKEN> -u <APP_KEY>:<APP_SECRET>

Note: APP_KEY & APP_SECRET are on the dropbox site for your account

3 Likes

Any hint about how to send a binary content? I’m trying the following Jsonata expression, but the content is sent with a leading "

$base64decode(body.image)

Just to give some feedback here for other users. I managed to use this tutorial to send an image to Dropbox. The trick was to use a Proxy route, so I can send my data as base64 to notehub.io and it will be unpacked and sent as a binary to dropbox.

Also, instead of using a Dropbox-API-Arg fixed header, I sent is as an URL parameter using the following code

  JAddStringToObject(req, "name", "?arg=%7B%22path%22%3A%22%2Ftest.jpeg%22%7D");  // ?arg={"path":"/test.jpeg"}

hope this helps

3 Likes