Exception while generating Access token: ZohoOAuthException. Caused by : Access Token has expired.

Exception while generating Access token: ZohoOAuthException. Caused by : Access Token has expired.

Following the guide from the developer console and using the Java SDK (2.0.1), I was able to generate an access token - but for some reason it throws an exception saying the token has expired.

It is quite clear from the logs (I have masked some properties):
  1. Dec 17, 2019 4:50:56 PM com.zoho.oauth.client.ZohoOAuth initialize
  2. INFO: Zoho OAuth Client Library configuration properties : {minLogLevel=ALL, currentUserEmail=support@een.com, client_secret=client_secret, redirect_uri=http://www.example.com/oauth2callback, iamURL=https://accounts.zoho.com, client_id=client_id, oauth_tokens_file_path=oauthtokens.properties, persistence_handler_class=com.zoho.oauth.clientapp.ZohoOAuthFilePersistence}
  3. Dec 17, 2019 4:50:56 PM com.zoho.crm.library.exception.ZCRMLogger logInfo
  4. INFO: ZCRM - Java Client Library configuration properties : {apiBaseUrl=https://www.zohoapis.com, photoUrl=https://profile.zoho.com/api/v1/user/, apiVersion=v2, minLogLevel=ALL, currentUserEmail=support@een.com, domainSuffix=com, client_secret=client_secret, redirect_uri=http://www.example.com/oauth2callback, client_id=client_id, oauth_tokens_file_path=oauthtokens.properties, persistence_handler_class=com.zoho.oauth.clientapp.ZohoOAuthFilePersistence}
  5. Dec 17, 2019 4:50:56 PM com.zoho.oauth.common.ZohoHTTPConnector post
  6. INFO: POST - URL = https://accounts.zoho.com/oauth/v2/token , HEADERS = {} , PARAMS = {code=code, grant_type=authorization_code, client_secret=client_secret, redirect_uri=http://www.example.com/oauth2callback, client_id=client_id} . 
  7. Dec 17, 2019 4:50:57 PM com.zoho.oauth.common.ZohoHTTPConnector post
  8. INFO: STATUS_CODE = 200 ,RESPONSE_JSON = {"access_token":"access_token","refresh_token":"refresh_token","api_domain":"https://www.zohoapis.com","token_type":"Bearer","expires_in":3600}
  9. com.zoho.oauth.common.ZohoOAuthException. Caused by : com.zoho.oauth.common.ZohoOAuthException. Caused by : Access Token has expired.
  10. at com.zoho.oauth.client.ZohoOAuthClient.generateAccessToken(ZohoOAuthClient.java:95)
  11. at com.pcmseu.zoho.api.AccountsApiImpl.getAccessToken(AccountsApiImpl.java:93)
  12. at com.pcmseu.zoho.api.AccountsApiImpl.getAccountById(AccountsApiImpl.java:54)
  13. at com.pcmseu.zoho.testlauncher.ZohoLauncher.main(ZohoLauncher.java:34)
In the logs, you could see that we get a success response and access token is generated - and I am able to use it too. But due to this exception, I believe it is not persisted on the oauthtokens.properties file. 
  1. com.zoho.oauth.common.ZohoOAuthException. Caused by : Access Token has expired.

Why is this exception thrown and how could we prevent this?

And could it handle the token generation under the hood using ZCRMRestClient.initialize(zcrmConfigurations) (without having to generate the token) ? I could see it is already done for C Library. What do we need to do this? When I tried this I get the below exception:

  1. Dec 17, 2019 5:38:23 PM com.zoho.crm.library.exception.ZCRMLogger logError
  2. SEVERE: ZCRM -  in com.zoho.crm.library.api.APIRequest.authenticateRequest:183 ::: com.zoho.oauth.common.ZohoOAuthException. Caused by : com.zoho.oauth.common.ZohoOAuthException. Caused by : java.lang.NullPointerException
  3. com.zoho.crm.library.exception.ZCRMException. Caused by : com.zoho.oauth.common.ZohoOAuthException. Caused by : com.zoho.oauth.common.ZohoOAuthException. Caused by : java.lang.NullPointerException
  4. at com.zoho.crm.library.api.APIRequest.authenticateRequest(APIRequest.java:184)
  5. at com.zoho.crm.library.api.APIRequest.getResponseFromServer(APIRequest.java:272)
  6. at com.zoho.crm.library.api.APIRequest.getAPIResponse(APIRequest.java:236)
  7. at com.zoho.crm.library.api.handler.EntityAPIHandler.getRecord(EntityAPIHandler.java:52)
  8. at com.zoho.crm.library.crud.ZCRMModule.getRecord(ZCRMModule.java:613)
  9. at com.pcmseu.zoho.api.AccountsApiImpl.getAccountById(AccountsApiImpl.java:61)
  10. at com.pcmseu.zoho.testlauncher.ZohoLauncher.main(ZohoLauncher.java:34)
  11. Caused by: com.zoho.oauth.common.ZohoOAuthException. Caused by : com.zoho.oauth.common.ZohoOAuthException. Caused by : java.lang.NullPointerException
  12. at com.zoho.oauth.client.ZohoOAuthClient.getAccessToken(ZohoOAuthClient.java:54)
  13. at com.zoho.crm.library.common.ZCRMConfigUtil.getAccessToken(ZCRMConfigUtil.java:88)
  14. at com.zoho.crm.library.api.APIRequest.authenticateRequest(APIRequest.java:173)
  15. ... 6 more