如何在Google Apps脚本中使用Dropbox刷新令牌将驱动器文件传输到Dropbox?
根据几个示例,我在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');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的目标如下。
您想将以下curl命令转换为Google Apps脚本。
curl
在这种情况下,以下修改如何?
修改后的脚本:
请设置您的
刷新,AppKey,AppSecret
的值。Grant_Type
和refresh_token
作为表单数据。而且,使用基本授权。注意:
参考:
I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script.
curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token= -u :
In this case, how about the following modification?
Modified script:
Please set your values of
refreshToken, appKey, appSecret
.grant_type
andrefresh_token
as the form data. And, the basic authorization is used.Note:
Reference: