One month down in 2025: How are your resolutions coming along? Check out how to get back on track here.
Forum Discussion
bnfofficial
10 months agoNew member | Level 2
0Auth2 keeps expiring despite refresh token
Hello everyone! I am not a developer by any means, but with the help of ChatGPT I managed to create a Google App Script that lets me mirror certain Google Drive Folders into my Dropbox. It almost works perfect, except that after a few hours the authentification expires even though I understood that with 0Auth2, new short-lived tokens can be accessed continously without having to manually authenticate again and again?
Could someone please point me to the flaw in the script (ChatGPT can't help anymore and thinks everything is fine)? Here is the relevant part of the script:
var DROPBOX_CLIENT_ID = "...";
var DROPBOX_CLIENT_SECRET = "...";
function authorize() {
var service = OAuth2.createService('dropbox')
.setAuthorizationBaseUrl('https://www.dropbox.com/oauth2/authorize')
.setTokenUrl('https://api.dropboxapi.com/oauth2/token')
.setClientId(DROPBOX_CLIENT_ID)
.setClientSecret(DROPBOX_CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('files.content.write')
.setGrantType('authorization_code');
var authorizationUrl = service.getAuthorizationUrl();
Logger.log;
Logger.log(authorizationUrl);
}
function authCallback(request) {
var service = OAuth2.createService('dropbox')
.setAuthorizationBaseUrl('https://www.dropbox.com/oauth2/authorize')
.setTokenUrl('https://api.dropboxapi.com/oauth2/token')
.setClientId(DROPBOX_CLIENT_ID)
.setClientSecret(DROPBOX_CLIENT_SECRET)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('files.content.write')
.setGrantType('authorization_code');
var isAuthorized = service.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput;
} else {
return HtmlService.createHtmlOutput;
}
}
function getDropboxAccessToken() {
var service = OAuth2.createService('dropbox')
.setPropertyStore(PropertiesService.getUserProperties())
.setTokenUrl('https://api.dropboxapi.com/oauth2/token')
.setClientId(DROPBOX_CLIENT_ID)
.setClientSecret(DROPBOX_CLIENT_SECRET)
.setScope('files.content.write')
.setGrantType('authorization_code');
var accessToken = service.getAccessToken();
if (!accessToken || service.hasAccess() && service.getAccessToken() === null) {
var refreshToken = getStoredRefreshToken();
service.refresh();
accessToken = service.getAccessToken();
if (accessToken) {
PropertiesService.getUserProperties().setProperty('DROPBOX_REFRESH_TOKEN', refreshToken); // Speichern des Refresh-Tokens
}
}
return accessToken;
}
function getStoredRefreshToken() {
return PropertiesService.getUserProperties().getProperty('DROPBOX_REFRESH_TOKEN');
}
- Greg-DB
Dropbox Staff
Dropbox currently offers short-lived access tokens, which expire after a few hours, and refresh tokens, which don't expire.
If you need continuous access without repeated manual interaction, you'll need to request "offline" access in order to get a refresh token. The refresh token can be used to programmatically retrieve new short-lived access tokens whenever needed.
We can't offer support for this particular "OAuth2.createService" utility as it's not written by Dropbox, so I suggest referring to the documentation and support resources for that for information on how to use and troubleshoot it. For instance, it's unclear if it automatically requests offline access in order to receive a refresh token (and I don't see your code doing so explicitly), so you should check on that to make sure you're getting, storing, and re-using a refresh token.
About Discuss Dropbox Developer & API
Make connections with other developers803 PostsLatest Activity: 4 hours ago
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!