If you were trying to consume user profile service in InfoPath and you were able to query it correctly in the preview mode of InfoPath , but you were getting error while querying it after you published it to SharePoint.
Please follow these steps to impersonate the appropriate permissions while querying the service.
1- Create Secure Store Service Application
• Go to central Administration-> Application Management -> Manage Service Application
• Click New Secure Store Service-> provide the appropriate names
• After the Secure store servicegot created , click on it , Generate a New Key and Enter the appropriatepassword.
• Click New Enter the TargetApplication ID , display name and Contact Email
• Choose in the targetApplication Type = Group
• u can click next on the page identifyingthe fields in the ID, leave the defaults.
• For the target applicationAdministrators choose Farm account.
• For the Members, add DomainUsers as the members. This will allow all users to be able to use the form andconnect to the User Profile Service. Click OK when completed.
• Once you have the ApplicationID created, click the key and from the drop down menu set the credentials ,type the farm account credentials.
2- Allow User from templates touse authentication information
• Go to General ApplicationSettings>Configure InfoPath Forms Services in Central Administration.
• Ensure that the checkbox nextto “Allow user from templates to use authentication information contained indata connection files” is checked.
3- Create a new Data ConnectionLibrary
• Navigate to the SharePointportal
• Click on Site Contents ->Add an app and create a new data connection library
4- Convert Data connection File
• Open InfoPath file
• Click on Manage DataConnections
• Choose the user profile dataconnection and click on convert to Connection File
• Go the data connection libraryand save your connection.
5- Modify the connection file
• Go the data connection library
• Download the newly createddata connection file
• Open it using notepad
• To connect with our webservice, Delete the comment dashes and exclamation points from the node in theXML and enter the app ID you have created and the credential type of NTLM. Itshould look like this Where the InfoPathID is the keyyou created in the secure store
• Upload the modified file tothe data connection library.
6- Get the logged-in User Data
• Create a new Field “Username”
• Add the following formula tothe Field “translate(substring-after(userName(), "|"),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")”
• In the query button Add firstthe following action Set “AccountName” from the QueryFields Group = the newField “Username”
• Add a new Action QueryUserProfileService
• Add your own Action wherevalue=AccountName