首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zoho图书API - Google Sheets

Zoho图书API - Google Sheets
EN

Stack Overflow用户
提问于 2021-05-05 07:00:12
回答 1查看 894关注 0票数 0

我将我的销售细节记录在谷歌页面上,并使用Zoho图书来维护我的账簿。我想使用Zoho在google和Zoho之间同步数据。到目前为止,我已经做了以下工作:

  1. 在Zoho控制台中创建一个自客户端,以生成客户机ID和客户端秘密
  2. 在Zoho控制台中的发票下为所有Scope生成一个授权代码
  3. 使用邮递员生成访问令牌和刷新令牌
  4. 用Google脚本编写了下面的代码,用虚拟数据创建发票
代码语言:javascript
复制
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.

不知道我哪里出了问题?

EN

回答 1

Stack Overflow用户

发布于 2021-05-05 08:58:20

修改要点:

  • 当我看到针对Books API的“创建发票”的正式文档时,示例curl命令似乎如下所示。 $ curl id=10234695 -X POST -H“授权: Zoho-oauthtoken -H -H”内容-Type: application/x-www-form-urlencoded;charset=UTF-8“-F 'JSONString="{,,,}"‘
代码语言:javascript
复制
- 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

代码语言:javascript
复制
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并再次测试它。

代码语言:javascript
复制
var params = {
  method: 'POST',
  payload: {JSONString: Utilities.newBlob(JSON.stringify(invoice), "application/json")},
  headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
  muteHttpExceptions: true,
};

模式2:

对于上面修改的脚本,请按以下方式修改params并再次测试它。从OP的测试中发现,这个示例请求是正确的.

代码语言:javascript
复制
var params = {
  method: 'POST',
  contentType: "application/json",
  payload: {JSONString: JSON.stringify(invoice)},
  headers: {Authorization: "Zoho-oauthtoken " + zohoOauthToken},
  muteHttpExceptions: true,
};

代码语言:javascript
复制
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命令来自我的答案的顶部。

代码语言:javascript
复制
$ 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脚本如下所示。

代码语言:javascript
复制
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());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67396500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档