api - Zoho Books API - Google 表格
问题描述
我正在 Google 表格中记录我的销售详细信息,并使用 Zoho Books 来维护我的账簿。我想使用 Zoho Books API 在 google 表格和 Zoho Books 之间同步数据。到目前为止,我已经做了以下事情:
- 在 Zoho API 控制台中创建了一个自助客户端以生成客户端 ID 和客户端密码
- 为 Zoho API 控制台中发票下的所有范围生成授权码
- 生成访问令牌和刷新令牌 - 使用 Postman
- 在 Google Apps 脚本中编写了以下代码以使用虚拟数据创建发票
function ZohoInvoice() {
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 = '1000.827612479824c7c66132118bb242e15942aa6a.4e63c9fd60a343658904a54191c4c32';
var zohoOrganization = '19012342064';
var zohoUrl = [
'https://books.zoho.com/api/v3/invoices?',
'organization_id=',
zohoOrganization,
'&authtoken=',
zohoOauthToken,
'&JSONString=',
encodeURIComponent(JSON.stringify(invoice)),
].join('');
try {
var response = UrlFetchApp.fetch(zohoUrl, {
method: 'POST',
muteHttpExceptions: true,
});
var result = JSON.parse(response.getContentText());
Logger.log(result.message);
} catch (error) {
Logger.log(error.toString());
}
}
上面的代码抛出一个错误Invalid value passed for authtoken。
不确定我哪里出错了?
解决方案
修改点:
当我看到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());