Zoho OAuth application workflow like Facebook login and Google login OAuth.
In this blog post. We will discuss this if a web developer wants the website user data store in Zoho CRM and web server database as well. Similar to a WordPress website has own users table in the database for storing user details.
Register your application with Zoho.
This code snippet about the Zoho application solution to save the user data in CRM without the user redirect on Zoho login and ask permission.
As a web developer, I hope you know how OAuth is working. According to OAuth the website user have to become a user of that particular third party website. Similar we did in Facebook login. No one can login on your website using Facebook login until he/she becomes a Facebook user.
Zoho OAuth has the same approach. But Zoho application providing a feature “Self Client”.
The “Self Client” feature provides access token and refresh token. The access token is not required but we need the refresh token in this code snippet.
The refresh token will expire after a few minutes. So we need to regenerate the refresh token using the application URL.
Please keep in mind the code using “.IN” domain application URL. You need to change your application domain URL according to your Zoho account.
Code snippet for getting a valid access token.
$client_id = 'ZohoApiClientId'; $client_secret = 'ZohoApiClientSecret'; $access_token = 'ZohoApiAccessToken'; $refresh_token = 'ZohoApiRefreshToken'; $curl = 'https://accounts.zoho.in/oauth/v2/token?refresh_token='.$refresh_token.'&client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=refresh_token'; $ch = curl_init($curl); $headers = array( 'Content-Type: application/json', ); curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// Turn off the server and peer verification curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response) curl_setopt($ch, CURLOPT_POST, 1);//Regular post curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $response = json_decode( $response ); echo '<pre>'; print_r( $response ); echo '</pre>'; echo isset( $response->access_token ) ? $response->access_token : '';
Code snippet for inserting the new lead into Zoho CRM.
if( isset( $response->access_token ) && $response->access_token != '' ){ /** * Lead data array. */ $lead_data['data'][0] = array( 'First_Name' => 'Lead First Name', 'Last_Name' => 'Lead Last Name', 'Email' => '[email protected]', ); /** * Bearer token will access_token. */ $headers = array( 'Content-Type: application/json', 'Authorization: Bearer '.$response->access_token ); $curl = 'https://www.zohoapis.in/crm/v2/Leads'; $ch = curl_init($curl); curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// Turn off the server and peer verification curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response) curl_setopt($ch, CURLOPT_POST, 1);//Regular post curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $lead_data ) );// Set the request as a POST FIELD for curl. $response = curl_exec($ch); $response = json_decode( $response ); echo '<pre>'; print_r( $response ); echo '</pre>'; echo $response->data[0]->details->id; // return CRM lead record id echo $response->data[0]->status; // success or error } else { echo 'Refresh Token is not valid'; }