![](/img/trans.png)
[英]Trouble using cURL with Google Apps Script Web App in a PHP file
[英]Google App Script Silently fails to POST, Curl Works
Curl正在發布,但Google AppScript的憑據不同。
我正在嘗試獲取谷歌應用程序腳本以將當前文檔作為html內容發布到新的Jive文檔中
//以下curl命令可以正常工作
curl -u USERNAMEHERE:PASSWORDHERE -H "Content-Type: application/json" --data '{ "type": "document", "content": { "type": "text/html", "text":"<h1>HOORAY</h1> a Document is born"}, "subject": "TEST WORKED"}' https://MYJIVEURL.com/api/core/v3/places/XXXXXXXX/contents
// Apps腳本現在拋出401並失敗
function pleaseWork() {
var encode = Utilities.base64Encode('USER:PASS', Utilities.Charset.UTF_8);
var docbody = DocumentApp.getActiveDocument().getBody();
var subject = DocumentApp.getActiveDocument().getName();
var url = "https://JIVEURL/api/core/v3/places/XXXXXX/contents";
var option = {
authorization: "Basic " + encode,
contentType: "application/json",
method: 'post',
payload: JSON.stringify({
subject: subject,
type: "document",
content: {
type: 'text/html',
text: docbody
},
})
}
var response = UrlFetchApp.fetch(url, option).getContentText()
}```
Theres no other errors to speak of in the AppScript editor. So I must be leaving something out. I just don't know what that is
第1部分- payload
params
對象具有一個稱為“有效負載”的參數,該參數應包含要作為字符串化JSON發送的數據。 因此,代替直接引用content
, subject
和type
,您應該執行以下操作(順便說一句,可以通過contentType
參數設置UrlFEtchApp
內容類型,並通過相應參數設置method
):
var option = {
//other parameters here;
method : 'post',
contentType : 'application/json',
payload : JSON.stringify( {
content : '',
subject : '',
type : ''
} )
}
第2部分- headers
盡管看似隨意,但並非所有參數都應移至params
對象的頂級屬性。 可以通過這種方式設置一組封閉的屬性(請參閱參考資料)。 授權仍應設置為header
,因此:
var option = {
//other parameters here;
headers : {
Authorization : 'Basic ' + yourAuth
},
method : 'post',
contentType : 'application/json',
payload : JSON.stringify( {
content : '',
subject : '',
type : ''
} )
}
有用的鏈接
UrlFetchApp.fetch()
參考 ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.