I am recording my sales details in Google Sheets and am using Zoho Books to maintain my books of accounts. I want to use Zoho Books API to sync data between google sheets and Zoho Books. I have done the following till now:
- Created a Self-Client in Zoho API Console to generate a Client ID and Client Secret
- Generated an authorization code for all Scopes under Invoices in the Zoho API Console
- Generated an access token and refresh token - using Postman
- Wrote the following code in Google Apps Script to create an invoice with dummy data
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());
}
}
The above code throws an error Invalid value passed for authtoken.
Unsure where am I going wrong?
Modification points:
When I saw the official document of "Create an invoice" for Books API, it seems that the sample curl command is as follows.
JSONString
might not be correctly parsed at the server side, because of the content type isapplication/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.When above points are reflected to your script, it becomes as follows. The following modified script is from above sample curl command.
Modified script:
Please set
zohoOauthToken
of your token.Note:
I think that above modified request is the same with the sample curl command for "Create an invoice". But, if above modified script occurs an error, please try the following patterns.
Pattern 1:For above modified script, please modify
Pattern 2:params
as follows and test it again.For above modified script, please modify
params
as follows and test it again. From OP's test, it was found that this sample request was correct.or
References:
Added:
When OP tested my proposed scripts, OP said the working script is
The first one under Pattern 2
. In this case, it seems that the sample curl command at the official document is not correct. WhenThe first one under Pattern 2
is converted to the curl command, it's as follows. In this sample curl command is from the top of my answer.The Google Apps Script is as follows.