Error while requesting for Access token using the Php SDK

Error while requesting for Access token using the Php SDK

Hi,

My name is Ashish. I have to collect the lead data from a custom form in my website. I have used the PHP SDK to store the lead data into CRM. 

I have a free account on ZOHO. I have added a client and get the Client ID and Secret from the Zoho application. Now i tried to get the access token. It gives me the below error.

  1. Uncaught zcrmsdk\oauth\exception\ZohoOAuthException Caused by:'zcrmsdk\oauth\exception\ZohoOAuthException Caused by:'Exception while fetching access token from grant token

ZohoV2.php
  1. <?php
    require 'vendor/autoload.php';

    use zcrmsdk\oauth\ZohoOAuth;

    use zcrmsdk\crm\crud\ZCRMInventoryLineItem;
    use zcrmsdk\crm\crud\ZCRMJunctionRecord;
    use zcrmsdk\crm\crud\ZCRMNote;
    use zcrmsdk\crm\crud\ZCRMRecord;
    use zcrmsdk\crm\crud\ZCRMModule;
    use zcrmsdk\crm\crud\ZCRMTax;
    use zcrmsdk\crm\setup\restclient\ZCRMRestClient;
    use zcrmsdk\crm\setup\users\ZCRMUser;

    use zcrmsdk\crm\crud\ZCRMCustomView;
    use zcrmsdk\crm\crud\ZCRMTag;
    use zcrmsdk\crm\exception\ZCRMException;

    class ZohoV2 {

    private $oAuthClient=null;
    private $configuration=null;
    public function __construct($conf=null) {

    if($conf!=null)
    $this->configuration=$conf;
    //Initialize Core SDK library
    ZCRMRestClient::initialize($this->configuration);
    $this->oAuthClient = ZohoOAuth::getClientInstance();

    }

    //For set all configurations
    public function setConfiguration($config){
    $this->configuration=$config;
    }

    // When after authorization you redirect you will get this grantToken and using that you will get access token
    public function generateToken($grantToken){
    //Below is sample grant token
    return $oAuthTokens = $this->oAuthClient->generateAccessToken($grantToken);
    }

    //This function if access token expire then using refresh token you can generate new Access token
    public function refreshToken($refreshToken,$userIdentifier){
    //below is sample refresh token you found and that automatically updated once generate
    return $oAuthTokens = $this->oAuthClient->generateAccessTokenFromRefreshToken($refreshToken,$userIdentifier);
    }

    //Update existing record
    public function updateRecord($zohoid,$data,$module="Contacts"){
    try{

    $zcrmRecordIns = ZCRMRecord::getInstance($module, $zohoid);

    foreach($data as $d=>$v){
    $zcrmRecordIns->setFieldValue($d, $v);
    }

    $entityResponse=$zcrmRecordIns->update();

    if("success"==$entityResponse->getStatus()){

    $createdRecordInstance=$entityResponse->getData();

    return $createdRecordInstance->getEntityId();
    echo "Status:".$entityResponse->getStatus();
    echo "Message:".$entityResponse->getMessage();
    echo "Code:".$entityResponse->getCode();

    echo "EntityID:".$createdRecordInstance->getEntityId();
    echo "moduleAPIName:".$createdRecordInstance->getModuleAPIName();

    }
    return "";
    }catch(Exception $e){
    $file_names = __DIR__."/zoho_ERROR_UPDATE_log.txt";
    file_put_contents($file_names, $e.PHP_EOL , FILE_APPEND | LOCK_EX);
    return "";
    }

    }
    //Create a new contact record
    public function newRecord($data,$module="Leads") {
    //if(count($data)<=0)return "";
    $records = [];
    try{

    $record = ZCRMRecord::getInstance($module, null );
    foreach($data as $d=>$v)
    $record->setFieldValue($d, $v);

    $records[] = $record;

    $zcrmModuleIns = ZCRMModule::getInstance($module);
    $bulkAPIResponse=$zcrmModuleIns->createRecords($records); // $recordsArray - array of ZCRMRecord instances filled with required data for creation.
    $entityResponses = $bulkAPIResponse->getEntityResponses();

    foreach($entityResponses as $entityResponse){
    if("success"==$entityResponse->getStatus()){
    $createdRecordInstance=$entityResponse->getData();
    return $createdRecordInstance->getEntityId();
    }
    else{
    $file_names = __DIR__."/zoho_ERROR_ADDNEW_log.txt";
    file_put_contents($file_names, (json_encode($entityResponses)).PHP_EOL , FILE_APPEND | LOCK_EX);
    return "-1";
    }
    }
    }catch(Exception $e){
    $file_names = __DIR__."/zoho_ERROR_ADDNEW_log.txt";
    file_put_contents($file_names, $e.PHP_EOL , FILE_APPEND | LOCK_EX);
    return "";
    }
    }

    // Get specific record by using id
    public function getRecordById($id=1000266000007206013,$module="Leads"){
    $zcrmModuleIns = ZCRMModule::getInstance($module)->getRecord($id);
    return $recordsArray = $zcrmModuleIns->getData();
    }
    }
    ?>
zoho_execute.php
  1. <?php

    include ("zohoV2.php");
    class ZOHO_Exec{

    public $zoho;
    private $client_id = "1000.9HL3B77TKUKO3XNON696SQWCB5ZEMV";
    private $client_secret = "b8053c00fb360ccd31a32dceeef39cfde497a67fc9";
    private $user_email_id = "xxx.xxx@yyy.com";
    private $sub_user_email_id = null;

    function __construct(){
    $configuration= array(
    "client_id" =>$this->client_id,
    "client_secret" =>$this->client_secret,
    "redirect_uri" =>$this->redirect_url,
    "accounts_url" =>"https://accounts.zoho.in",
    "currentUserEmail" =>$this->user_email_id,
    "access_type" =>"offline",
    "persistence_handler_class" =>"ZohoOAuthPersistenceHandler",
    "token_persistence_path" =>__DIR__."/",
    "applicationLogFilePath" =>__DIR__."/"
    //"user_email_id" =>"<SUB USER>"

    );
    $this->connect_zoho($configuration);
    }

    private function connect_zoho($conf){
    $this->zoho = new ZohoV2($conf);
    return true;
    }

    public function zoho_data($row=null){

    /* Here you need to map your ZOHO fields with value more customization please contact parbat@gatetouch.com for any PHP framwork or in CMS */

    if(empty($row)){
    $row = array(
    'First_Name'=>'xxx',
    'Last_Name'=>'xxxxxx',
    'Email'=>'xxx@xxx@yyyy.com',
    'Company'=>'Gatetouch',
    'Mobile'=>'1111111111',
    'Mailing_Street'=>'Sample Street',
    'Mailing_City'=>'Test City',
    'Mailing_Zip'=>'444444',
    'Mailing_State'=>'Test State',
    'Country'=>'India',
    'Subscription_Status'=>'Active',
    'Lead_Source'=>'website',
    );
    }
    return $row;
    }

    private function zohoupdate($row=null,$zohoid=null){
    // Get ZOHO data
    $lead = $this->zoho_data($row);
    if(!empty($zohoid) && intval($zohoid)>0) {
    $this->zoho->updateRecord( $zohoid,$lead);
    } else {
    //new registration
    $zohoid = $this->zoho->newRecord($lead);
    $this->set_zoho_id($row, $zohoid);
    }
    }

    private function set_zoho_id($row,$zohoid){
    /*
    Here you can update your record with new zoho ID so next time you can update zoho record
    */
    }
    }

    ?>
  2. //Now calling above class object
    $exeZ =new ZOHO_Exec();
    $grantToken = "1000.65ea7c775424be5e92cded7a9b5447c8.675939ddf8e32e6a367cfe2a2fc662f8";
    $accessToken = $exeZ->zoho->generateToken($grantToken);
    //Now going to add record in contact module
    var_dump($accessToken);

Grant token has been genrated from the Zoho Self Client. 

Request you to please help me on this.