The
app would be ready to be initialized after defining the 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.
- Self-Client
We will be using the self-client option here to demonstrate the process.
Generating self-authorized grant and refresh token
- Visit https://accounts.zoho.com/developerconsole
- Click → 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 a time of expiry.
- Provide “aaaserver.profile.READ” scope along with Zoho CRM scopes.
All the scopes that are required must be given in the syntax: ZohoCRM.{scope_name}.{operation_type}.
- 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 choose while generating it. Hence, the access and refresh
tokens should be generated within that time.
Generating 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. Please 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";
- $oAuthTokens = $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.