3

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'};
}

3 Answers 3

2

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 the inputHttpHeaders_x map.

E.g.

CaseCreationUtility.CS_CaseCreationUtility stub = new CaseCreationUtility.CS_CaseCreationUtility();
ts.SessionHeader = new CaseCreationUtility.SessionHeader_element();
ts.SessionHeader.sessionId = oauth.access_token;

You may also need to adjust the endpoint URL if it differs from the Org that it was generated from.

// You will need to set the instance (here na5) 
// to match the origin of the sessionId
ts.endpoint_x = 'https://na5.salesforce.com/services/Soap/class/OptionalNamespaceHere/CaseCreationUtility';

The requested oAuth session id should have the full or api 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 with UserInfo.getSessionId().

0
1

For SOAP requests the session header shuold be like that

stub.inputHttpHeaders_x.put('SessionId', oauth.access_token);

Session header is ready by key "sessionid"

Sample Header Request:

  <soapenv:Header>
     <urn:SessionHeader>
        <urn:sessionId><b>QwWsHJyTPW.1pd0_jXlNKOSU</b></urn:sessionId>
     </urn:SessionHeader>
  </soapenv:Header>
7
  • I'm still getting the same error after applying your said changes: Line: 164, Column: 1 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=
    – sanchit
    Feb 18, 2015 at 7:06
  • Do we need to obtain the session id explicitly?
    – sanchit
    Feb 18, 2015 at 7:08
  • 1
    I think you should obtain session Id by using login method. Access token will not work with soap api Feb 18, 2015 at 7:51
  • 1
    @sanchit 1. Make sure that you are not getting session Id using getSessionId() in a batch. 2. Server URL must be the instance url which you want to integrate. 3. Connected app access scope must be properly defined.
    – Ashwani
    Feb 18, 2015 at 8:13
  • @AtulRajguru9 do we need to make changes in the existing wsdl for performing the login? As per this reference: salesforce.com/us/developer/docs/apexcode/Content/… we are passing access token via HTTP headers..!
    – sanchit
    Feb 18, 2015 at 8:19
1

All i know is that on @future functions and Batchable code The session ID you obtain when you perform a UserInfo.getSessionId() call. So you must pass it through a parameter.

Not sure if this info is the one you need, it's all i can provide to help!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .