Dropbox server side

Hello everybody,

I would like to use dropbox in a “cron” function so only on the server side.
I already use dropbox client side and everything is working properly.

client side:
let Dropbox = require ('dropbox'). Dropbox;
let dbx = new Dropbox ({accessToken: <my_key>, fetch});

from “dbx” I can use all dropbox APIs

If I do the same thing on the server side, I get the error
“ReferenceError: fetch is not defined”

Can anyone help me solve this error
Thank you
YC

2 Likes

Is it because the SDK assumes HTML5 fetch API is available?

You’ll also need to generate an access token because OAuth is strictly client-side.

You can then use something like superagent to make requests:

let result = await superagent.post('https://api.dropboxapi.com/2/users/get_account')
               .set('Authorization', 'Bearer ' + access_token)
               .type('application/json')
               .send({"account_id": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc"});

for example. You can also use Meteor’s http package for that but I found superagent to be a bit more convenient.

thank you for your reply.

I’m trying to understand how to use the code you offer.
Is ‘result’ a dropbox object like ‘dbx’ on the client side and can be used as for example

result.filesUpload ({content: text, path: ......

No, result contains the return value of the API call you made.

I see this NPM covers Node and browsers: https://www.npmjs.com/package/dropbox. You would be interested in the Node side of the SDK.

Does this mean that you need to authenticate for each API call?
Could you please, give me an example of code for example to use the API ‘filesListFolder’ which allows to recover the directories of a path.

Thanks for your help

let data = {
    "path": "/example/folder",
    "recursive": false,
    "include_media_info": false,
    "include_deleted": false,
    "include_has_explicit_shared_members": false,
    "include_mounted_folders": true,
    "include_non_downloadable_files": true
};
let result = await superagent.post('https://api.dropboxapi.com/2/files/list_folder')
               .set('Authorization', 'Bearer ' + access_token)
               .type('application/json')
               .send(data);

//example JSON return value of result:

{
    "entries": [
        {
            ".tag": "file",
            "name": "Prime_Numbers.txt",
            "id": "id:a4ayc_80_OEAAAAAAAAAXw",
            "client_modified": "2015-05-12T15:50:38Z",
            "server_modified": "2015-05-12T15:50:38Z",
            "rev": "a1c10ce0dd78",
            "size": 7212,
            "path_lower": "/homework/math/prime_numbers.txt",
            "path_display": "/Homework/math/Prime_Numbers.txt",
            "sharing_info": {
                "read_only": true,
                "parent_shared_folder_id": "84528192421",
                "modified_by": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc"
            },
            "is_downloadable": true,
            "property_groups": [
                {
                    "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
                    "fields": [
                        {
                            "name": "Security Policy",
                            "value": "Confidential"
                        }
                    ]
                }
            ],
            "has_explicit_shared_members": false,
            "content_hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        },
        {
            ".tag": "folder",
            "name": "math",
            "id": "id:a4ayc_80_OEAAAAAAAAAXz",
            "path_lower": "/homework/math",
            "path_display": "/Homework/math",
            "sharing_info": {
                "read_only": false,
                "parent_shared_folder_id": "84528192421",
                "traverse_only": false,
                "no_access": false
            },
            "property_groups": [
                {
                    "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
                    "fields": [
                        {
                            "name": "Security Policy",
                            "value": "Confidential"
                        }
                    ]
                }
            ]
        }
    ],
    "cursor": "ZtkX9_EHj3x7PMkVuFIhwKYXEpwpLwyxp9vMKomUhllil9q7eWiAu",
    "has_more": false
}

I’m almost there…
I installed superagent with “meteor npm install superagent”

In main.js server side I have the code

const superagent = require('superagent');

console.log(receipeCoordsGPS());

async function receipeCoordsGPS() {
	let data = {
		"path": "Maréa°yvancoyaud@free.fr/LESBLOGS",
		"recursive": false,
		"include_media_info": false,
		"include_deleted": false,
		"include_has_explicit_shared_members": false,
		"include_mounted_folders": true,
		"include_non_downloadable_files": true
	};
	try {
		return await superagent.post('https://api.dropboxapi.com/2/files/list_folder')
			.set('Authorization', 'Bearer ' + 'EVI0bxu9TpAAAAA....... my application token ')
			.type('application/json')
			.send(data);
	}catch(e) {
		return ('Unexpected error occurred');
	}
}

At runtime I have the error

=> App running at: http: // localhost: 3000 /
I20191116-07: 13: 11,919 (11)? Promise {<pending>}

Why ‘pending’ ?

Oops … …
My apologies I was wrong! Everything is working properly
Thank you very much for your help.

So while I’m there, I did not think anything like that ‘FileReader’ side server to decode text files. I use client side but server side I have the error
I20191116-22: 18: 49.605 (11)? ReferenceError: FileReader is not defined
Although it works on the client side, you have to add a package to use server-side fileReader
Otherwise I will open a new topic.

Thank you
YC

Server side you use the built-in file methods.

https://nodejs.org/api/fs.html

OK, I found.
I realized that on the server side you have to work with the NodeJs APIs.:roll_eyes:
Everything is working properly.

Thanks a lot for your help.
YC