I created an Apex class (global) containing Webservice methods-> generated WSDL out of it->consumed it in a different org. While instantiating the webservice callout, I set the http header to contain the authorization information as Authorization=>Bearer + Access Token.
Just wanted to clarify here, I obtained the Access Token via connected app (username, password+token, consumer secret and client id) and after successfully obtaining it I passed that as an HTTP header before calling the generated wsdl method as follows:
CaseCreationUtility.CS_CaseCreationUtility stub = new
CaseCreationUtility.CS_CaseCreationUtility();
stub.inputHttpHeaders_x = new Map<String, String>();
//Setting a bearer authentication header
stub.inputHttpHeaders_x.put('Authorization', 'Bearer '+oauth.access_token);
I tried the following as well but no luck:
stub.inputHttpHeaders_x.put('Authorization', 'Basic
'+oauth.access_token); stub.inputHttpHeaders_x.put('Authorization',
'OAuth '+oauth.access_token);
Finally, the error comes when the webservice method is called as follows:
stub.createCase('CS_Order','1234','','','','','','','','','','','','','','','',''); // ==> GENERATNG the error: System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor=
It seems there's something wrong with the autogenerated Apex class as its not able to create/set the session required for communication.
Does it mean I need to use the Enterprise/Partner WSDL rather than
just downloading the WSDL directly for the Apex class containing
webservice methods?
If not, could you please look into the following WSDL file that I used:
Apex Class Wsdl
After consuming the above WSDL, following wrapper class is autogenerated regarding Session,
Is there anything wrongly parsed or anything wrong?
public class SessionHeader_element {
public String sessionId;
private String[] sessionId_type_info = new String[]{'sessionId','http://soap.sforce.com/schemas/class/CS_CaseCreationUtility',null,'1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://soap.sforce.com/schemas/class/CS_CaseCreationUtility','true','false'};
private String[] field_order_type_info = new String[]{'sessionId'};
}
Best Answer
You will need to correctly set the
SessionHeader
on the instance of CaseCreationUtility.CS_CaseCreationUtility. You don't need to set the session id in theinputHttpHeaders_x
map.E.g.
You may also need to adjust the endpoint URL if it differs from the Org that it was generated from.
The requested oAuth session id should have the
full
orapi
scopes to be able to access the web service. The session should be to the same org where the Apex web service is implemented.If it doesn't work initially with the oAuth derived session, try getting the web session id from the
sid
cookie or via anonymous Apex withUserInfo.getSessionId()
.