我一直在使用Google Apps脚本在Harvest中生成发票。这个剧本充当了魅力,但突然之间就不再存在了。
我用来发布到Harvest的功能:
function postInvoiceToHarvest(xml){
var USERNAME = PropertiesService.getScriptProperties().getProperty('username');
var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');
var url = "https://acme.harvestapp.com/invoices/";
var headers = {
"Accept": "application/xml",
"Content-Type": "application/xml",
"Authorization": "Basic "+ Utilities.base64Encode(USERNAME+":"+PASSWORD)};
var options = {
"method" : "post",
"headers" : headers,
"payload" : xml,
"muteHttpExceptions": true};
var response = UrlFetchApp.fetch(url,options);
Logger.log('Response Code: ' + response.getResponseCode());
}
日志:
UrlFetchApp.fetch([https://acme.harvestapp.com/invoices/, {headers={Authorization=Basic xxxxxx, Content-Type=application/xml, Accept=application/xml}, payload=<?xml version="1.0" encoding="UTF-8"?>
<invoice>
<client-id type="integer">123</client-id>
<currency>Euro - EUR</currency>
<subject>Invoice</subject>
<notes>My Note</notes>
<number>123</number>
<kind>subscription</kind>
<csv-line-items>"kind","description","quantity","unit_price","amount","taxed","taxed2","project_id"
"subscription","subscription 1",11,6,66,false,false,0</csv-line-items>
</invoice>
, method=post}]) [0,784 seconden]
这失败了。看起来xml有效负载不再能正常发送。
我做错了什么?感谢您的支持!
答案 0 :(得分:0)
我能够让它发挥作用!事实证明,你必须对你的标题非常特别。
var invoicePayload = JSON.stringify(invoice);
var options = {
'headers': {
"Accept": "application/json", // required!
'Authorization': 'Basic ' + Utilities.base64Encode(user + ':' pass),
},
contentType: "application/json", // required!
contentLength: (invoicePayload.length + 2).toString(), // required!
// (added 2 for quotes, seemed to be other clients did this and it worked
'method': 'post',
'payload': invoicePayload, // json string
followRedirects: false,
muteHttpExceptions: true
};
UrlFetchApp.fetch("https://subdomain.harvestapp.com/invoices", options);
您也可以使用XML执行此操作,只需将内容/接受更改为application / xml,并将内容长度更改为匹配。这是这里的三个关键标题。排除&#34;接受&#34;将重定向到登录页面,不包括内容类型或长度将给你一个&#34;客户端ID是必需的&#34;消息。