如何在 Google Apps 脚本中使用 Dropbox 刷新令牌将云端硬盘文件传输到 Dropbox?

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

基于几个示例,我在 Google 应用程序脚本中编写了一个函数,它接受给定的输入文件并将其保存在 Dropbox 上的指定位置。它使用从 Dropbox 应用程序控制台获得的短期访问令牌来执行此操作...

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

这只会工作 4 小时,所以我通过标准流程获得了应用程序的刷新令牌,并且通过终端使用这个 cURL 命令我能够生成新的工作 SL 令牌。

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

我的问题是将该 CURL 命令转换为 Google 应用程序 Javascript 并提取返回的 SL 令牌。这是我的尝试...

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

我的错误响应是“{error_description=缺少必填字段“grant_type”,error=unsupported_grant_type}”确认我的问题是误解了格式,但我'我不确定如何修复它。

修复该问题后,我将从响应中解析出 SL 令牌,然后将其与上面的工作文件上传代码一起使用。格式是否有明显的遗漏?

编辑:这是基于所选答案的工作函数。这可能应该分为两个功能。

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

我相信你的目标如下。

这样的话,下面的修改怎么样?

修改后的脚本:

请设置 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);
  • 当我看到您的示例 curl 命令时,似乎需要将 grant_typerefresh_token 作为表单数据发送。并且,使用基本授权。

注:

  • 在此答案中,假设您的示例 curl 命令有效。请注意这一点。

参考: