修改点:
当我看到Books API的“创建发票”的官方文档时,似乎示例curl命令如下。
$ curl https://books.zoho.com/api/v3/invoices?organization_id=10234695
-X POST
-H "Authorization: Zoho-oauthtoken 1000.41d9f2cfbd1b7a8f9e314b7aff7bc2d1.8fcc9810810a216793f385b9dd6e125f"
-H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
-F 'JSONString="{,,,}"'
- 在这种情况下,令牌需要包含在请求标头中。
- 但是,我认为在这个 curl 命令中,
JSONString
可能无法在服务器端正确解析 的值,因为内容类型是application/x-www-form-urlencoded
. 所以我不确定这个官方文档的示例 curl 命令是否正确。我认为在这种情况下,-H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
可能不需要使用。我很担心这个。因此,请测试以下修改后的脚本。
当以上几点反映到您的脚本时,它变成如下。以下修改后的脚本来自上面的示例 curl 命令。
修改后的脚本:
请设置zohoOauthToken
您的令牌。
var invoice = {
customer_id: '2298656000000277003',
invoice_number: 'MU001',
date: '2021-09-02',
line_items: [
{
item_id: '2298656000002380000',
name: 'Item1',
description: 'This is the description',
rate: '1500.00',
quantity: '2',
},
],
notes: 'These are the notes of this Invocie'
};
var zohoOauthToken = '###';
var zohoOrganization = '19012342064';
var baseUrl = "https://books.zoho.com/api/v3/invoices";
var url = baseUrl + "?organization_id=" + zohoOrganization;
var params = {
method: 'POST',
contentType: "application/x-www-form-urlencoded",
payload: {JSONString: Utilities.newBlob(JSON.stringify(invoice), "application/json")}, // or null instead of "application/json"
headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
muteHttpExceptions: true,
};
var response = UrlFetchApp.fetch(url, params);
console.log(response.getContentText());
笔记:
我认为上述修改后的请求与“创建发票”的示例 curl 命令相同。但是,如果上面修改的脚本出现错误,请尝试以下模式。
模式一:
对于以上修改的脚本,请params
进行如下修改并再次测试。
var params = {
method: 'POST',
payload: {JSONString: Utilities.newBlob(JSON.stringify(invoice), "application/json")},
headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
muteHttpExceptions: true,
};
模式二:
对于以上修改的脚本,请params
进行如下修改并再次测试。从 OP 的测试中,发现这个示例请求是正确的。
var params = {
method: 'POST',
contentType: "application/json",
payload: {JSONString: JSON.stringify(invoice)},
headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
muteHttpExceptions: true,
};
或者
var params = {
method: 'POST',
contentType: "application/json",
payload: JSON.stringify(invoice),
headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
muteHttpExceptions: true,
};
参考:
添加:
当 OP 测试我提出的脚本时,OP 说工作脚本是The first one under Pattern 2
. 在这种情况下,似乎官方文档中的示例 curl 命令不正确。当The first one under Pattern 2
转换为curl命令时,如下。在此示例 curl 命令中,来自我的答案。
$ curl https://books.zoho.com/api/v3/invoices?organization_id=10234695
-X POST
-H "Authorization: Zoho-oauthtoken 1000.41d9f2cfbd1b7a8f9e314b7aff7bc2d1.8fcc9810810a216793f385b9dd6e125f"
-H "Content-Type: application/json;charset=UTF-8"
-F 'JSONString="{,,,}"'
Google Apps 脚本如下。
var invoice = {
customer_id: '2298656000000277003',
invoice_number: 'MU001',
date: '2021-09-02',
line_items: [
{
item_id: '2298656000002380000',
name: 'Item1',
description: 'This is the description',
rate: '1500.00',
quantity: '2',
},
],
notes: 'These are the notes of this Invocie'
};
var zohoOauthToken = '###';
var zohoOrganization = '19012342064';
var baseUrl = "https://books.zoho.com/api/v3/invoices";
var url = baseUrl + "?organization_id=" + zohoOrganization;
var params = {
method: 'POST',
contentType: "application/json",
payload: {JSONString: JSON.stringify(invoice)},
headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
muteHttpExceptions: true,
};
var response = UrlFetchApp.fetch(url, params);
console.log(response.getContentText());