Whenever the app is started, the below code snippet is to be called for initialization.
- var ZCRMRestClient = require('zcrmsdk');
- ZCRMRestClient.initialize().then(function()
- {
- //do whatever required after initialize
- })
Generating self-authorized grant and refresh token
- 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
- 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.
Each
time server is restarted, this function has to be called and both the
configuration files should be populated with proper values before
calling this function, else exception will be thrown.
All functions return promises in zcrm node sdk.
Getting access and refresh tokens from grant token through method calls
- 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);
- });
The access and refresh tokens are generated. In case the access token is expired, the SDK will refresh it automatically.
If the user has refresh token and need to generate access token below function can be used,
- ZCRMRestClient.generateAuthTokenfromRefreshToken(user_identifier,refresh_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);
- });
Sample API call for getting Leads:
- var input ={};
- input.module = "Leads";
- var params = {};
- params.page = 0;
- params.per_page = 5;
- input.params = params;
- zcrmsdk.API.MODULES.get(input).then(function(response){
- var result = "<html><body><b>Leads</b>";
- var data = response.body;
- data = JSON.parse(data);
- data = data.data;
- for (i in data){
- var record = data[i];
- var name = record.Full_Name;
- result+="<span>"+name+"</span>";
- }
- result+="</body></html>";
- })