Search code examples
google-apps-scriptfile-uploadgoogle-drive-apidropboxdropbox-api

How Can I use Dropbox Refresh Tokens in Google Apps Script to transfer Drive Files to Dropbox?


Based on several examples, I wrote a function in Google apps script that takes a given input file and saves it at the specified location on Dropbox. It does this using a short lived access token obtained from the Dropbox apps console...

function driveToDropbox(inputFile, targetFilePath, writeMode='add') {
  //inputFile = Drive file object, targetFilePath = string, writeMode = 'overwrite' or 'add'
  inputFile = DriveApp.getFilesByName('temp.html').next(); //for testing
  targetFilePath = '/temp.html' //for testing

  var apiUrl = 'https://content.dropboxapi.com/2/files/upload';
  var dropboxAccessToken = '<SL Token>';

  var parameters = {
    path: targetFilePath,
    mode: writeMode,
    autorename: true,
    mute: false,
    strict_conflict: false,
  };

  var headers = {
    'Content-Type': 'application/octet-stream',
    Authorization: 'Bearer ' + dropboxAccessToken,
    'Dropbox-API-Arg': JSON.stringify(parameters),
  };

  var options = {
    method: 'POST',
    headers: headers,
    payload: inputFile.getBlob().getBytes(),
  };

  var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
  //Logger.log(response);

  Logger.log('File uploaded successfully');
}

This will only work for 4 hours, so I obtained the refresh token for the app through the standard flow, and with this cURL command via terminal I am able to generate new working SL tokens.

curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=<R Token> -u <App key>:<App secret>

My problem is converting that CURL command into Google apps Javascript and pulling the returned SL token. This was my attempt...

var apiUrl = 'https://api.dropbox.com/oauth2/token';
var refreshToken = '<R Token>';
var appKey = '<App key>';
var appSecret = '<App secret>';
var appKeySecret = '<Combined app key+secret>'

var parameters = {
  refresh_token: refreshToken,
  client_id: appKey,
  client_secret: appSecret,
};

var headers = {
  'Content-Type': 'application/json',
  Authorization: 'Basic ' + appKeySecret
  'Dropbox-API-Arg': JSON.stringify(parameters),
};

var options = {
  method: 'POST',
  headers: headers,
  grant_type: "refresh_token",
  muteHttpExceptions: true,
};

var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
Logger.log(response);

My error response is "{error_description=missing required field "grant_type", error=unsupported_grant_type}" confirming that my issue is misunderstanding the formatting, but I'm not sure how to fix it.

After fixing that, I would parse out the SL token from the response and then use that with the working file upload code above. Is there something obvious that I am missing with the formatting?

EDIT: Here is the working function based on the selected answer. This should probably be broken into two functions.

function driveToDropbox(inputFile, targetFilePath, writeMode='add') {
  //inputFile = Drive file object, targetFilePath = string, writeMode = 'overwrite' or 'add'
  inputFile = DriveApp.getFilesByName('temp.html').next(); //for testing
  targetFilePath = '/temp.html'; //for testing

  ////Obtain new 4 hour SL access token with Refresh token
  var refreshToken = '<R Token>';
  var appKey = '<App key>';
  var appSecret = '<App secret>';

  var apiUrl = 'https://api.dropbox.com/oauth2/token';
  var options = {
    method: 'POST',
    headers: { "Authorization": "Basic " + Utilities.base64Encode(`${appKey}:${appSecret}`) },
    payload: {
      grant_type: "refresh_token",
      refresh_token: refreshToken
    },
    muteHttpExceptions: true,
  };
  var response = UrlFetchApp.fetch(apiUrl, options);
  var accessToken = JSON.parse(response.getContentText()).access_token;

  ////Transfer file with refreshed SL access token
  apiUrl = 'https://content.dropboxapi.com/2/files/upload';
  var parameters = {
    path: targetFilePath,
    mode: writeMode,
    autorename: true,
    mute: false,
    strict_conflict: false,
  };

  options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/octet-stream',
      Authorization: 'Bearer ' + accessToken,
      'Dropbox-API-Arg': JSON.stringify(parameters),
    },
    payload: inputFile.getBlob().getBytes(),
  };

  response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());

  Logger.log('File uploaded successfully');
}

Solution

  • I believe your goal is as follows.

    In this case, how about the following modification?

    Modified script:

    Please set your values of refreshToken, appKey, appSecret.

    var refreshToken = '<R Token>';
    var appKey = '<App key>';
    var appSecret = '<App secret>';
    
    var apiUrl = 'https://api.dropbox.com/oauth2/token';
    var options = {
      method: 'POST',
      headers: { "Authorization": "Basic " + Utilities.base64Encode(`${appKey}:${appSecret}`) },
      payload: {
        grant_type: "refresh_token",
        refresh_token: refreshToken
      },
      muteHttpExceptions: true,
    };
    var response = UrlFetchApp.fetch(apiUrl, options);
    var obj = JSON.parse(response.getContentText());
    var accessToken = obj.access_token; // You can retrieve the access token.
    
    console.log(accessToken);
    
    • When I saw your sample curl command, it seems that it is required to send grant_type and refresh_token as the form data. And, the basic authorization is used.

    Note:

    • In this answer, it supposes that your sample curl command works. Please be careful about this.

    Reference: