我将我的销售细节记录在谷歌页面上,并使用Zoho图书来维护我的账簿。我想使用Zoho在google和Zoho之间同步数据。到目前为止,我已经做了以下工作:
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.。
不知道我哪里出了问题?
发布于 2021-05-05 08:58:20
修改要点:
- In this case, the token is required to be included in the request header.
- But, I thought that in this curl command, the value of `JSONString` might not be correctly parsed at the server side, because of the content type is `application/x-www-form-urlencoded`. So I'm not sure whether this sample curl command of this official document is correct. I thought that in this case, `-H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"` might not be required to be used. I'm worry about this. So, please test the following modified script.当以上要点反映到您的脚本中时,如下所示。下面修改的脚本来自上面的示例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命令相同。但是,如果上面修改的脚本发生错误,请尝试以下模式。
模式1:
对于上面修改的脚本,请按以下方式修改params并再次测试它。
var params = {
method: 'POST',
payload: {JSONString: Utilities.newBlob(JSON.stringify(invoice), "application/json")},
headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
muteHttpExceptions: true,
};模式2:
对于上面修改的脚本,请按以下方式修改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脚本如下所示。
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());https://stackoverflow.com/questions/67396500
复制相似问题