I was able to fix the issue.
The points where I was missing are:
The LinkedIn Application whose API Key and Secret I was using to retrieve data was configured to ask only BASIC_PROFILE permission (This was because LinkedIN recently introduced some changes in their API Permissions). NETWORK permissions were also required to access connections info.
In code, I should have used
ProfileField.RELATION_TO_VIEWER_RELATED_CONNECTIONS
instead ofProfileField.RELATION_TO_VIEWER
Following is the final piece of code that worked for me:
LinkedInAccessToken targetUserLiAccessToken = new LinkedInAccessToken(targetUserOauthToken, targetUserOauthSecret)
LinkedInApiClient targetUserLiApiClient= linkedInApiClientFactory.createLinkedInApiClient(targetUserLiAccessToken)
Person targetUserLiProrfile=targetUserLiApiClient.getProfileForCurrentUser([ProfileField.ID] as Set)
LinkedInAccessToken currentUserLiAccessToken = new LinkedInAccessToken(currUserOauthToken, currUserOauthSecret)
LinkedInApiClient currentUserLiApiClient= linkedInApiClientFactory.createLinkedInApiClient(currentUserLiAccessToken)
Person resultProfile = currentUserLiApiClient.getProfileById(targetUserProfile.id, [ProfileField.ID, ProfileField.RELATION_TO_VIEWER_RELATED_CONNECTIONS] as Set)
List<Person> commonConnections= resultProfile.relationToViewer.relatedConnections.personList