[SalesForce] Reading Metadata API from APEX via Force.com Public Site

I've been able to use this Metadata API built on APEX platform:
https://github.com/financialforcedev/apex-mdapi

But it is working if I install this into the org, and user log into the org. WHat I want is to access the API using force.com Site, in which I pass on the session id via URL and session ID is readable from parameters and the metadata listing works for that outside org.

I've modified the MetadataService.cls file and make sure that Session ID is "not" UserInfo.getSessionId(), but is passed into the CreateService Function as input parameter and wherever I call CreateService I pass on the session ID from page parameters.

Earlier I was getting this particular error on the Visualforce Page exposed on Sites:

System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: UNKNOWN_EXCEPTION: Site under construction faultcode=UNKNOWN_EXCEPTION faultactor=

To resolve this, I edited the Service Endpoint to the instance url of the Org whose SessionId I'm passing in this page.

But I'm receiving this error now:

WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor=

Does anyone know a way to make this Metadata API work on public sites?

Best Answer

It is always worth checking the Apex Debug log when making callouts, to confirm visually the SOAP XML request (which the platform dumps to the log) is passing what you expect from your code. Enable the Apex Debug Log and check the session Id is being passed correctly.

A debug log can record database operations, system processes, and errors that occur when executing a transaction or running unit tests. Debug logs can contain information about: Database changes HTTP callouts Apex errors Resources used by Apex Automated workflow processes, such as: Workflow rules Assignment rules Approval processes Validation rules

Related Topic