Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

amei's avatar
amei
Explorer | Level 3
4 months ago
Solved

Using javascript to upload file to Dropbox

I am developing an html file for our group to do weekly picks online. I have to program working where the users can pick their own games. However I need a place to store and retrieve the information selected so other users can view current picks. I want to use Dropbox as my online repository. Through my internet searches, I created an app in Dropbox and made it available to anyone with the link. However when I try to implement a function to upload the file I get the following error :

Here is what the html looks like. The save button for each line will become active once there are valid picks for all games. At this point the file (in this case an array with selected picks) should be uploaded to the dropbox api. Other users (7) will need to be able to upload and download files (only done by javascript code) to upload and download the most current selection information.

Here is the api that I have created :

app Key

App Secret

Here is the function I am trying to use to do this :

        async function saveFileToDropBox() {
                const dbx = new Dropbox.Dropbox({ accessToken: '' });

                var text_array = JSON.stringify(users_array)
                var file = new File([text_array], "users_array.json", { type: "application/json" });
                var dropbox_path_file = '/2025/users_array.json';
                try {
                    const response = await dbx.filesUpload({
                        path: dropbox_path_file, // Desired path and filename in Dropbox
                        contents : file ,
                        mode: { '.tag': 'overwrite' } // Optional: Overwrite if file exists
                });
                console.log('File uploaded successfully:', response);
                } catch (error) {
                console.error('Error uploading file:', error);
                }
        }

It seems like my problem is with the dropbox authorization. I generated the access token from the app settings page. Am I missing something in the authorization process. Any code updates would be greatly appreciated. Thank you in advance.

  • amei​ 

    If the 'files.content.write' scope was enabled after the access token was generated, you’ll need to generate a new access token. Permission changes don’t apply retroactively to previously issued tokens.

6 Replies

Replies have been turned off for this discussion
  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 months ago

    Hi amei​ 

    I'll be happy to help with any issues you're having with the Dropbox API, but I'll need some more information. Could you share the error message being returned along with the 401 code?

    For reference, a 401 error can occur for a number of reasons, you can refer to our error documentation for additional details on what might be causing the issue.

  • amei's avatar
    amei
    Explorer | Level 3
    4 months ago

    Hello DB-Des

    Thanks for you response. I had included a screenshot as well as my function that uploads to dropbox in my initial post. I will provide them again, and try to include more detail. In my html console this is the error message I receive (screenshot below) :

    I have a function that is utilized once the user has made selections on all games and clicks the Save button. This begins the upload attempt to my dropbox app. See details of my dropbox app in my initial post. I wanted to include this in case there was an issue with how I setup my dropbox api. 

    Here is my function that I use to attempt to upload an array (contains information on user picks) to dropbox. Note I left out the actual access token code. All I currently use for authentication is the API access Token. I am not sure how to properly authenticated users to upload to dropbox. 

    async function saveFileToDropBox() {
                    const dbx = new Dropbox.Dropbox({ accessToken: '' " });

                    var text_array = JSON.stringify(users_array)
                    var file = new File([text_array], "users_array.json", { type: "application/json" });
                    var dropbox_path_file = '/2025/users_array.json';
                    try {
                        const response = await dbx.filesUpload({
                            path: dropbox_path_file, // Desired path and filename in Dropbox
                            contents : file ,
                            mode: { '.tag': 'overwrite' } // Optional: Overwrite if file exists
                    });
                    console.log('File uploaded successfully:', response);
                    } catch (error) {
                    console.error('Error uploading file:', error);
                    }
            }

    Hopefully this along with screenshots of my app settings provides enough information. I tried clicking on the error to get more information about it :

    Only first part of access token

     

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 months ago

    amei​ 

    Errors from the API are typically accompanied by a message that gives more context about what went wrong. We’ll need to see that message in order to better assist you.

    In your code's 'try...catch' block, you are logging the error to your console:

    ...
        catch (error) {
            console.error('Error uploading file:', error);
        }
    ...

    Looking at your console should provide you more insight into the error you are receiving. One of the properties will be the 'error_summary'.

    To further narrow it down, you can console 'error.error' or 'error.error.error_summary'.

  • amei's avatar
    amei
    Explorer | Level 3
    4 months ago

    I tried your suggestion about error.error in the console. The 1st error description was an expired access token which I refreshed. The 2nd error description was to enable files.content.write, which I enabled. The 3rd error description (latest), shown below :

    The following are my app permissions :

     

  • DB-Des's avatar
    DB-Des
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 months ago

    amei​ 

    If the 'files.content.write' scope was enabled after the access token was generated, you’ll need to generate a new access token. Permission changes don’t apply retroactively to previously issued tokens.

  • amei's avatar
    amei
    Explorer | Level 3
    4 months ago

    When I updated my access token I received the message below : 

     

    But when I looked in the dropbox folder in the app I did not see the file there., however it does appear in recents. Mabye there is a delay as I do see it in the folder a little later. I also notice that my access token expires after a day and I have to keep generating a new one. Is there a way to make it last longer (e.g. one month) ?

     

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!