Forum Discussion

TStack's avatar
TStack
New member | Level 1
2 months ago

App Script & Dropbox: Expired Access Token Issue - Need Help with Long-Lived Token

Hi Dropbox Community,

I've created a Google Apps Script to automatically send copies of modified Excel files as CSV files to my Dropbox. The script uses a Dropbox App Folder (created in the App Console) and works by using a Dropbox access token. The App is simple and doesn't require Redirect URIs, Webhook URIs, etc.

My script (snippet below) worked perfectly for several hours, relying solely on the access token:

function onEdit(e) {
  // Configuration
  const ACCESS_TOKEN = 'DropBox_Access_Token';
  const DROPBOX_FOLDER_PATH = 'Folder_Path';
  const FILE_NAME = 'data.csv'; // Name of the CSV file in Dropbox
  // ... (rest of the script)
}
However, after some time, it stopped working with this error:

Error uploading file to Dropbox: Exception: Request failed for https://content.dropboxapi.com returned code 401. Truncated server response: {"error_summary": "expired_access_token/", "error": {".tag": "expired_access_token"}} (use muteHttpExceptions option to examine full response)

This clearly indicates the access token has expired. The script's purpose is to run automatically without manual intervention.

My question is: how to get long-lived access token and how can I modify my Apps Script to use a long-lived access token or a refresh token mechanism to prevent this token expiration issue? 

Any guidance or examples would be greatly appreciated.

Thanks in advance for your help!

  • iNeil's avatar
    iNeil
    Icon for Dropbox Engineer rankDropbox Engineer

    Hello TStack,

    Dropbox is no longer offering the option for creating new long-lived access tokens. Dropbox is now issuing short-lived access tokens (and optional refresh tokens) instead of long-lived access tokens. You can find more information on this migration here.

    Apps can still get long-term access by requesting "offline" access though, in which case the app receives a "refresh token" that can be used to retrieve new short-lived access tokens as needed, without further manual user intervention. Refresh tokens do not expire automatically and can be used repeatedly. You can find more information in the OAuth Guide and authorization documentation. There's a basic outline of processing this flow in this blog post which may serve as a useful example.

  • TStack's avatar
    TStack
    New member | Level 1

    Solution Found: Automating Sending Google Sheets to Dropbox

    We've come up with two efficient methods for automating the upload of Google Sheets as CSV files to Dropbox, using Google Apps Script.

    Approach 1: Short-lived Access Token

    Utilizes short-lived access tokens that need manual renewal. Follow the script instructions for Dropbox app setup and trigger configuration.

    Approach 2: Refresh Access Token

    Implements refresh tokens for automatic renewal, enhancing security and reducing maintenance. Detailed instructions for app setup and authorization are included.

    For code examples and further details, check out the full guide at TSSFL Technology Stack: https://www.tssfl.com/viewtopic.php?t=6879

    Feel free to modify the solutions as needed!

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,951 PostsLatest Activity: 3 hours ago
352 Following

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 or Facebook.

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!