Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve README formatting #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 67 additions & 96 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,126 +1,107 @@
##Node JS SDK for Zoho CRM
# Node JS SDK for Zoho CRM

##Abstract
## Abstract

Node SDK is a wrapper for Zoho CRM APIs. Hence invoking a Zoho CRM API from your Node application is just a function call which provide the most appropriate response.

This SDK supports both single user as well as multi user authentication.

##Registering a Zoho Client
## Registering a Zoho Client

Since Zoho CRM APIs are authenticated with OAuth2 standards, you should register your client app with Zoho. To register your app:

- Visit this page https://accounts.zoho.com/developerconsole.
- Visit this page https://accounts.zoho.com/developerconsole.
- Click on “Add Client ID”.
- Enter Client Name, Client Domain and Redirect URI.
- Select the Client Type as "Web based".
- Click “Create”.
- Your Client app would have been created and displayed by now.
- The newly registered app's Client ID and Client Secret can be found by clicking Options → Edit. (Options is the three dot icon at the right corner).

- Click on “Add Client ID”.

- Enter Client Name, Client Domain and Redirect URI.

- Select the Client Type as "Web based".

- Click “Create”.

- Your Client app would have been created and displayed by now.

- The newly registered app's Client ID and Client Secret can be found by clicking Options → Edit.
(Options is the three dot icon at the right corner).

##Installation of Node CRM SDK
## Installation of Node CRM SDK

Node JS SDK will be installed and a package named 'zcrmsdk' will be created in the installation directory.

>npm install zcrmsdk
```console
npm install zcrmsdk
```

Once installed it can be used in the code as below,

>var ZCRMRestClient = require('zcrmsdk')

##API Usage
```javascript
var ZCRMRestClient = require('zcrmsdk')
```

##Configurations
## Configurations

Your OAuth Client details should be given to the SDK as a property file. In the SDK, you need to configure a file named oauth_configuration.properties. Please place the respective values in that file. You can place it under resources/ package from where the SDK is used.


zcrmsdk will try reading file from **'resources/oauth_configuration.properties'**

zcrmsdk will try reading file from `'resources/oauth_configuration.properties'`

Please fill the values for the following keys alone.
Based on your domain(EU,CN), please change the value of crm.iamurl. Default value set as US domain.


```

```ini
[zoho]
crm.iamurl=
crm.clientid=
crm.clientsecret=
crm.redirecturl=

crm.iamurl=
crm.clientid=
crm.clientsecret=
crm.redirecturl=
```

crm.clientid, crm.clientsecret and crm.redirecturl are your OAuth client’s configurations that you get after registering your Zoho client.
crm.iamurl is the accounts URL. It may be accounts.zoho.com or accounts.zoho.eu. If the crm.iamurl is not specified, by default the URL will be accounts.zoho.com.
`crm.clientid`, `crm.clientsecret`, and `crm.redirecturl` are your OAuth client’s configurations that you get after registering your Zoho client. `crm.iamurl` is the accounts URL. It may be `accounts.zoho.com` or `accounts.zoho.eu`. If `crm.iamurl` is not specified, the default URL will be `accounts.zoho.com`.

In configuration.properties file:
In `configuration.properties` file:

```
```ini
[crm]
api.url=
api.user_identifier=
api.tokenmanagement=
api.url=
api.user_identifier=
api.tokenmanagement=

[mysql]
username=
password=

password=
```
api.url is the URL used to call APIs. By default, the URL is www.zohoapis.com.
api.user_identifier will be empty by default. For single user authentication, this key can be filled with the respective email id, so that all calls happens by using this user's authentication.
api.tokenmanagement is given as a measure to manage and maintain tokens. If tokenmanagement is not provided, then sdk's default implementation(mysql) will be followed.
username and password can be given here if you already have one created for your MySQL.
The above keys specified in configuration.properties file are all optional.

user_identifier can be set in two ways .
1.Mentioning it in api.user_identifier in configuration.properties file
2.Can be set via code using set setUserIdentifier.
`api.url` is the URL used to call APIs (default `www.zohoapis.com`).
`api.user_identifier` will be empty by default. For single user authentication, this key can be filled with the respective email id so that all API calls use this user's authentication.
`api.tokenmanagement` can be used to provide an alternative token storage function. If not specified the default implementation (MySQL) will be used.
`username` and `password` are the credentials used to access MySQL
The above keys specified in `configuration.properties` file are all optional.

If user_identifier is not set via both the ways then default value 'zcrm_default_user' will be set by the sdk itself .
`user_identifier` can be set by either:
1. Specifying `api.user_identifier` in the `configuration.properties` file or
1. Via code by using set `setUserIdentifier()`

If `user_identifier` is not set the default value (`zcrm_default_user`) will be used.

##Token Storage Mechanism

To use the default token storage provided by the SDK, the following are to be done:
## Token Storage Mechanism

**Mysql should be running in default port in localhost.**
To use the default token storage mechanism:
**MySQL should be running on the default port (3306) on localhost.**
Create a database named `zohooauth` with a table named `"oauthtokens"` with the columns `"useridentifier" (varchar), "accesstoken" (varchar), "refreshtoken" (varchar), and "expirytime" (bigint)`.

Database with name **zohooauth** should be created and a table with below configurations should be present in the database. Table, named **"oauthtokens"**, should have the columns **"useridentifier" (varchar) "accesstoken" (varchar), "refreshtoken" (varchar) and "expirytime" (bigint)**.
With this configuration in place, storage and retrieval of tokens will be handled by the SDK.

Once the configuration is set, storage and retrieval of tokens will be handled by the SDK.
If the user wants to utilize their own mechanism, they can mention it in configuration.properties by providing the respective module in api.tokenmanagement.
### Custom Token Storage
If you want to use another token storage mechanism, you can declare it in `configuration.properties` by specifying the respective module in `api.tokenmanagement`.

This module should contain the below methods,
**saveOAuthTokens(token_obj)**
**updateOAuthTokens(token_obj)**
Irrespective of response, the next execution happens. So care should be taken by the user in handling their module.
**getOAuthTokens()**
The expected response for this method : JSON array containing json response with expirytime, refreshtoken and accesstoken fields.
This module should contain the methods:
- `saveOAuthTokens(token_obj)`
- `updateOAuthTokens(token_obj)`
- `getOAuthTokens()` - shall return a JSON array containing `expirytime`, `refreshtoken` and `accesstoken` fields.


##Generating self-authorized grant and refresh token
## Generating self-authorized grant and refresh token

For self client apps, the self authorized grant token should be generated from the Zoho Developer Console (https://accounts.zoho.com/developerconsole)


- Visit https://accounts.zoho.com/developerconsole

- Click Options → Self Client of the client for which you wish to authorize.

- Enter one or more (comma separated) valid Zoho CRM scopes that you wish to authorize in the “Scope” field and choose the time of expiry. Provide “aaaserver.profile.READ” scope along with Zoho CRM scopes.
- Copy the grant token for backup.

- Generate refresh_token from grant token by making a POST request with the URL below https://accounts.zoho.com/oauth/v2/token?code={grant_token}&redirect_uri={redirect_uri}&client_id={client_id}&client_secret={client_secret}&grant_type=authorization_code

- Copy the refresh token for backup.
- Visit https://accounts.zoho.com/developerconsole
- Click Options → Self Client of the client for which you wish to authorize.
- Enter one or more (comma separated) valid Zoho CRM scopes that you wish to authorize in the “Scope” field and choose the time of expiry. Provide “aaaserver.profile.READ” scope along with Zoho CRM scopes.
- Copy the grant token for backup.
- Generate refresh_token from grant token by making a POST request with the URL below https://accounts.zoho.com/oauth/v2/token?code={grant_token}&redirect_uri={redirect_uri}&client_id={client_id}&client_secret={client_secret}&grant_type=authorization_code
- Copy the refresh token for backup.

Please note that the generated grant token is valid only for the stipulated time you chose while generating it. Hence, the access and refresh tokens should be generated within that time.

Expand All @@ -129,7 +110,7 @@ Each time server is restarted, this function has to be called and both the confi
**All functions return promises in zcrm node sdk.**


##Initialize
## Initialize

Below snippet has to be called before starting the app

Expand All @@ -141,24 +122,21 @@ ZCRMRestClient.initialize().then(function(){
...

});

```

##Generating access and refresh token from granttoken
## Generating access and refresh token from granttoken

```

ZCRMRestClient.generateAuthTokens(user_identifier,grant_token).then(function(auth_response){

console.log("access token :"+auth_response.access_token);
console.log("refresh token :"+auth_response.refresh_token);
console.log("expires in :"+auth_response.expires_in);

});

```

##Generating access token from refresh token
## Generating access token from refresh token

This will be handled by sdk itself if the access and refresh token is generated by sdk.Developer need not call this explicitly.

Expand All @@ -170,10 +148,9 @@ ZCRMRestClient.generateAuthTokenfromRefreshToken(user_identifier,refresh_token).
console.log("expires in :"+auth_response.expires_in);

});

```

##Sample API Calls
## Sample API Calls

```
var input ={};
Expand All @@ -194,7 +171,7 @@ crmclient.API.MODULES.get(input).then(function(response){

var record = data[i];
var name = record.Full_Name;

result+="<br><span>"+name+"</span>";

}
Expand All @@ -206,7 +183,7 @@ crmclient.API.MODULES.get(input).then(function(response){



##Hierarchy
## Hierarchy
zcrmsdk

```
Expand Down Expand Up @@ -248,20 +225,14 @@ zcrmsdk
executeFunctionsInPost
```


As appearing in the hierarchy, zcrmsdk entity class has instance variables to fetch its own properties and to fetch data of its immediate child entities through an API call.

For example, to call an API to get module data, the request should be zcrmsdk.API.MODULES.{operation_type}. The operation types can be GET, POST, PUT, DELETE or CREATE.



##Response Handling
## Response Handling
All API calls will give the actual API response given by Zoho APIs, except file download.

For file download, the response will contain an extra field filename.

##Error Handling:
All errors will be thrown explicitly and care should be taken in catching the same.



## Error Handling:
All errors will be thrown explicitly and care should be taken to catch them.