I am getting this HTTP error:
Status=Unauthorized, StatusCode=401
When I try and access the Tooling REST API from an Lightning component Apex controller being used inside Salesforce, running as System Administrator.
This is the HTTP GET Request Endpoint
https://x--x.my.salesforce.com/services/data/v44.0/tooling/query?q=Select+Id,ActiveVersion.VersionNumber,LatestVersion.VersionNumber,DeveloperName+From+FlowDefinition+ORDER+BY+DeveloperName
This is the method executing the request:
public static HttpResponse executeCallout(String method, String resourceName, String requestBody) {
HttpRequest request = new HttpRequest();
if (method == 'PATCH') {
request.setMethod('POST');
} else {
request.setMethod(method);
}
request.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + resourceName);
request.setHeader('Authorization', 'Bearer '+ UserInfo.getSessionId());
request.setHeader('Accept', 'application/json');
request.setHeader('X-PrettyPrint', '1');
request.setHeader('Content-Type', 'application/json');
if (String.isNotBlank(requestBody)) {
request.setBody(requestBody);
}
return new Http().send(request);
}
Where the method is equal to GET and requestBody is null and resourceName is equal to:
/services/data/v44.0/tooling/query?q=Select+Id,ActiveVersion.VersionNumber,LatestVersion.VersionNumber,DeveloperName+From+FlowDefinition+ORDER+BY+DeveloperName
I've checked the length of UserInfo.getSessionId() and it's returning a value of length 112
Questions
- What is causing the problem?
- How can I fix it?