问题描述
我正在 Google 表格中记录我的销售详细信息,并使用 Zoho Books 维护我的账簿。我想使用 Zoho Books API 在 google sheet 和 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());
}
}
上面的代码抛出一个错误为 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',headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},};
模式2:
对于上面修改过的脚本,请修改params
如下,再测试一下。 从OP的测试中,发现这个示例请求是正确的。
var params = {
method: 'POST',contentType: "application/json",payload: {JSONString: JSON.stringify(invoice)},};
或
var params = {
method: 'POST',payload: JSON.stringify(invoice),};
参考:
添加:
当 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',params);
console.log(response.getContentText());