Initialization

Initialization

The app would be ready to be initialized after defining the OAuth configuration configuration array. The user can now proceed to generate the required tokens to run the app.

The generation of the grant token can be done using two methods.

We will be using the self-client option here to demonstrate the process.

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)

  1. Go to Zoho Developer Console

  2. Click OptionsSelf Client of the client for which you wish to authorize.

  3. Enter one or more (comma separated) valid Zoho CRM scopes, that you wish to authorize, in the Scope field and choose a time of expiry. Provide aaaserver.profile.READ scope along with Zoho CRM scopes.

  4. Copy the grant token for backup.

  5. 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
  6. Copy the refresh token for backup.

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

Note

The access and refresh tokens are environment-specific and domain-specific. When you handle various environments and domains such as Production, Sandbox, or Developer and IN, CN, US, EU, or AU, respectively, you must use the access token and refresh token generated only in those respective environments and domains. The SDK throws an error, otherwise.
For example, if you generate the tokens for your Sandbox environment in the CN domain, you must use only those tokens for that domain and environment. You cannot use the tokens generated for a different environment or a domain.

Generating an access token

Access token can be generated by grant token or refresh token. Following any one of the two methods is sufficient.

Generating Access token from Grant token

The following code snippet should be executed from your main class to get access and refresh tokens. Paste the copied grant token in the string literal mentioned below. This is a one-time process.

$configuration =array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
$oAuthClient = ZohoOAuth::getClientInstance();
$grantToken = "paste_the_self_authorized_grant_token_here";
$oAuthTokens = $oAuthClient->generateAccessToken($grantToken);

Please note that the above code snippet is valid only once per grant token. Upon successful execution of the above code snippet, the generated access and refresh tokens would have been persisted through our persistence handler class.

Generating Access token from Refresh token

The following code snippet should be executed from your main class to get access and refresh tokens. Please paste the generated refresh token in the string literal mentioned below. This is a one-time process.

$configuration =array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);
$oAuthClient = ZohoOAuth::getClientInstance(); 
$refreshToken = "paste_the_refresh_token_here"; 
$userIdentifier = "provide_user_identifier_like_email_here"; 
$oAuthClient->generateAccessTokenFromRefreshToken($refreshToken,$userIdentifier);
 

Upon successful execution of the above code snippet, the generated access token and given refresh token would have been persisted through our persistence handler class.

Once the OAuth tokens have been persisted, subsequent API calls would use the persisted access and refresh tokens. The PHP SDK will take care of refreshing the access token using refresh token, as and when required.

App Startup

The PHP SDK requires the following line of code invoked every time your client app is started.

$configuration =array("client_id"=>{client_id},"client_secret"=>{client_secret},"redirect_uri"=>{redirect_url},"currentUserEmail"=>{user_email_id});
ZCRMRestClient::initialize($configuration);

Once the PHP SDK has been initialized by the above line, you could use any APIs of the SDK to get proper results.

Share this post : FacebookTwitter

Still can't find what you're looking for?

Write to us: support@zohocrm.com