基于幾個示例,我在 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=missing required field "grant_type", error=unsupported_grant_type}" 確認我的問題是誤解了格式,但我不知道如何解決它。
修復該問題后,我將從回應中決議出 SL 令牌,然后將其與上面的作業檔案上傳代碼一起使用。格式有什么明顯的我遺漏的嗎?
uj5u.com熱心網友回復:
我相信你的目標如下。
您想將以下 curl 命令轉換為 Google Apps 腳本。
卷曲https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token= -u :
在這種情況下,如何進行以下修改?
修改后的腳本:
請設定您的值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_type和refresh_token作為表單資料。并且,使用基本授權。
筆記:
- 在這個答案中,它假設您的示例 curl 命令有效。請注意這一點。
參考:
- 獲取(網址,引數)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/481054.html
標籤:谷歌应用脚本 上传文件 谷歌驱动API 保管箱 保管箱 API
上一篇:函式中的鍵值問題
