The app would be ready to be initialized after defining the OAuth configuration file.
Generating self-authorized grant and refresh token
- Visit https://accounts.zoho.com/developerconsole
- Click Options → 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.
- Copy the grant token for backup.
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
- 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.
- ZCRMRestClient::initialize();
- $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.
- ZCRMRestClient::initialize();
- $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.
- ZCRMRestClient::initialize();
Once the PHP SDK has been initialized by the above line, you could use any APIs of the SDK to get proper results.