如何在Google Apps脚本中使用Dropbox刷新令牌将驱动器文件传输到Dropbox?

发布于 2025-01-31 18:56:51 字数 3909 浏览 4 评论 0 原文

根据几个示例,我在Google Apps脚本中编写了一个函数,该函数获取给定的输入文件并将其保存在Dropbox上的指定位置。它使用从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');
}

这仅能工作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 Apps 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');
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2025-02-07 18:56:52

我相信您的目标如下。

  • 您想将以下curl命令转换为Google Apps脚本。

    curl

在这种情况下,以下修改如何?

修改后的脚本:

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

注意:

  • 在此答案中,它假设您的示例卷曲命令有效。请注意。

参考:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文