I'm attempting to use the AJAX Toolkit to access the current Account ID of the logged-in user from within a community site. (The particular community is managed by Nimble AMS, and is referred to as "Community Hub." It appears to be a fairly straightforward force.com community site.) The reason I need to access the Account ID is that I need to pass the current user's account ID to an embedded widget that lives in one of our Community Hub pages (a Wufoo form, to be specific).
At the advice of my implementation team, I am using the AJAX Toolkit. As a proof of concept, I'm calling the following code in the $(document).ready()
handler (paraphrasing a bit here):
function getAccountId() {
sforce.connection.sessionId = window.ApiUtils.getSessionId();
sforce.connection.serverUrl = '/communityhub/services/Soap/u/46.0';
try{
var userInfo = sforce.connection.getUserInfo();
console.log(userInfo);
}
catch(error) {
console.log(error);
}
}
However, I'm getting an error message in the console indicating that the current user (I'm logged in as a regular Community Hub user) doesn't have permission to access the API:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode>sf:INVALID_SESSION_ID</faultcode>
<faultstring>INVALID_SESSION_ID: This session is not valid for use with the API</faultstring>
<detail>
<sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault">
<sf:exceptionCode>INVALID_SESSION_ID</sf:exceptionCode>
<sf:exceptionMessage>This session is not valid for use with the API</sf:exceptionMessage>
</sf:UnexpectedErrorFault>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
I have tried enabling the 'API Access' permission in the Profile for "Community Hub User," which is the profile attached to the user that I'm logged in as, but the error message continues.
So here's the question. How can I overcome that error? What else, besides enabling API Access, do I need to do to allow this user to make an API call through the AJAX Toolkit to retrieve the current Account ID?