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');
}