在下面的函式中,我收到了來自 Twitter 的錯誤回應,即“錯誤請求”。請幫我解決這個問題。
//param1 file === DriveApp.getFileById()
//param2 init === returned object of initTwitterUpload function
//param3 service === OAuth1.createService()
function appendTwitterUpload(file,init,service) {
var blob = file.getBlob()
var bytes = blob.getBytes()
var base64Blob = Utilities.base64Encode(bytes)
var metaData = {name: file.getName(),
mimeType: file.getMimeType()}
var baseUrl = "https://upload.twitter.com/1.1/media/upload.json" //?command=APPEND&media_id="
//Oauth1percentEncode(init["media_id_string"]) "&segment_index=" Oauth1percentEncode(0);
var options = {method:'POST',
contentType : 'multipart/form-data',
payload : {'command':'APPEND',
'media_id' : init["media_id"],
'segment_index': 0,
},
files:{'media': bytes},
muteHttpExceptions:true}
var response = service.fetch(baseUrl,options);
return JSON.parse(response.getContentText())
}
我很感激快速修復。任何發現錯誤的 Twitter 開發人員,我都發現了一個帶有 Google 應用程式腳本的錯誤。我正在使用 OAuth 1.0a 身份驗證并正確遵循檔案中提到的所有程式。請為上述功能提供確切的修復。我正在使用 Urlfetchapp 發布內容型別為 multipart/form-data 的有效負載。這里的服務引數只不過是 urlfetchapp 與Oauth 1 庫中提供的所需身份驗證標頭

/ Twitter 檔案上傳--init -->append -->finalize /initTwitterUpload 功能運行良好。服務引數是一個帶有授權標頭的 urlfetchapp,并與oauth1 庫中的一個函式匹配(獲取)
我的 gs 檔案中的其他功能:
function uploadTwitterMedia(mediaUrl){
var file = DriveApp.getFileById(mediaUrl.replace("https://drive.google.com/open?id=",""))
var initResponse = initTwitterUpload(mediaUrl,service)
var appendResponse = appendTwitterUpload(file,initResponse,service)
var finalizeResponse = finalizeTwitterUpload(initResponse,service)
return initResponse["media_id_string"]
}
function initTwitterUpload(url,service){
var file = DriveApp.getFileById(url.replace("https://drive.google.com/open?id=",""))
var type = file.getMimeType()
var size = file.getSize()
var baseUrl = "https://upload.twitter.com/1.1/media/upload.json?"
var oauthParams = "command=INIT&total_bytes=" encodeURIComponent(size).replace(/\!/g, "!")
.replace(/\*/g, "*")
.replace(/\'/g, "'")
.replace(/\(/g, "(")
.replace(/\)/g, ")") "&media_type=" encodeURIComponent(type).replace(/\!/g, "!")
.replace(/\*/g, "*")
.replace(/\'/g, "'")
.replace(/\(/g, "(")
.replace(/\)/g, ")") "&media_category=" Oauth1percentEncode("TWEET_IMAGE");
var tweetUrl = baseUrl oauthParams
var response = service.fetch(tweetUrl,{method:'POST'})
return JSON.parse(response.getContentText())
}
function finalizeTwitterUpload(init,service){
var baseUrl = "https://upload.twitter.com/1.1/media/upload.json?"
var params = "command=FINALIZE&media_id=" Oauth1percentEncode(init["media_id_string"])
var tweetUrl = baseUrl params
var response = service.fetch(tweetUrl,{method:'POST',
muteHttpExceptions:true})
return JSON.parse(response.getContentText())
}
function Oauth1percentEncode(text){
text = encodeURIComponent(text).replace(/\!/g, "!").replace(/\*/g, "*").replace(/\'/g, "'")
.replace(/\(/g, "(");
return text
}
此外,statusTwitterUpload 功能沒有按預期作業,它給出的回應為“找不到頁面”。我感謝任何幫助。
uj5u.com熱心網友回復:
我找到了答案。感謝我
function appendTwitterUpload(file,init,service) {
var options = null
var response = null
var baseUrl = "https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=" init["media_id_string"]
"&segment_index=" Oauth1percentEncode(0);
var boundary = "xxxxxxxxxx";
var data = "";
data = "--" boundary "\r\n";
data = "Content-Disposition: form-data; name=\"media\"; filename=\"" file.getName() "\"\r\n";
data = "Content-Type:" file.getMimeType() "\r\n\r\n";
//retry fracemork for drive service error
var getPayload = partial (function(data,file,boundary){ return Utilities.newBlob(data).getBytes()
.concat(file.getBlob().getBytes())
.concat(Utilities.newBlob("\r\n--" boundary "--").getBytes());},data,file,boundary)
var payload = executeWithRetry(getPayload,3)
var options = {
method : "post",
contentType : "multipart/form-data; boundary=" boundary,
payload : payload,
muteHttpExceptions: true,
}
var response = service.fetch(baseUrl,options);
console.log("response---_>", response.getResponseCode());
return response.getResponseCode()
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.2/angular.min.js"></script>
Replace appendTwitterUpload with the function in the answer
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/452943.html
