Background
I am trying to access the Salesforce Reporting REST API.
public with sharing class SalesforceReportApi {
private final static String REPORTS_RESOURCE = '/services/data/v44.0/analytics/reports/';
private static HttpResponse execute(String method, String restResource) {
HttpRequest request = new HttpRequest();
request.setMethod(method);
request.setEndpoint(endpoint + restResource);
request.setTimeout(120000); // 2 Minutes
request.setHeader('Accept', 'application/json');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionID());
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
request.setCompressed(false);
HttpResponse httpResponse = new Http().send(request);
return httpResponse;
}
private static String endpoint {
get {
return URL.getOrgDomainUrl().toExternalForm();
}
}
public static MatrixReport getMatrixReport(Id reportId) {
HttpResponse response = execute(HttpMethod.GET, REPORTS_RESOURCE + reportId);
return (MatrixReport) JSON.deserialize(response.getBody(), MatrixReport.class);
}
}
The request produced is:
Method: GET
URL: https://x--x.cs83.my.salesforce.com/services/data/v44.0/analytics/reports/XXXX
But I am getting this error:
{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}
I thought that since the recent release using URL.getOrgDomainUrl()
meant to access the REST API meant I didn't need to authenticate.
Sandbox org is on Spring '19 Patch 19.4
Questions
- What am I doing wrong?
- How can I fix it?
NOTE: I fixed this using Named Credentials
you can read more here
null
? If you are seeing that in debug logs, then you won't be able to see the value there. Debug logs do not publish this value.