function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Neha@SfdcNeha@Sfdc 

unable to provide credentials in apex callouts

Hi all,

i need to invoke a webservice that can be accssed by specifying the credentials.
However i do not find an appropriate way to specify the credentials 
in my WSDLToApex class.Below is my Auto generated WSDLToApex class
 
//Generated by wsdl2apex

public class mmmmmSfdcOrdersinbound {
    public class OrderResponse_element {
        public String External_Reference_xc;
        public String SAP_SalesOrder_Id_xc;
        private String[] External_Reference_xc_type_info = new String[]{'External_Reference__c','urn:mmmmm:sfdc:ordersinbound',null,'0','1','true'};
        .....
	........

    public class Order_Line_Item_xc {
        public Integer Line;
        public String Product_xc;
        public String Product_Description_xc;
        public Double Quantity_xc;
        public String Expected_Delivery_date_xc;
        public String Sales_Unit_xc;
        public Boolean FOC_xc;
        private String[] Line_type_info = new String[]{'Line','urn:mmmmm:sfdc:ordersinbound',null,'0','1','true'};
        ......
 	......

    public class OrderRequest_element {
        public String CreatedBy;
        public String LastModifiedBy;
        public String Owner;
        public String RecordType;
        public String Account_Owner_xc;
        public String Business_Partner_xc;
        public String Contact_Person_xc;
        public String Delivery_date_xc;
        public String Description_xc;
        public String External_Reference_xc;
        public Double Net_Value_xc;
        public String Partner_Id_xc;
        public String Sales_Organisation_xc;
        public String Status_xc;
        public String Visit_xc;
        public String Wholesaler_xc;
        public mmmmmSfdcOrdersinbound.Order_Line_Item_xc[] Order_Line_Item_xc;
        private String[] CreatedBy_type_info = new String[]{'CreatedBy','urn:mmmmm:sfdc:ordersinbound',null,'0','1','true'};
        private String[] LastModifiedBy_type_info = new String[]{'LastModifiedBy','urn:mmmmm:sfdc:ordersinbound',null,'0','1','true'};
        .........
	.........

    public class Soap {
        public String endpoint_x = 'XXXxXXXXXXXX';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'urn:mmmmm:sfdc:ordersinbound', 'mmmmmSfdcOrdersinbound'};

        public mmmmmSfdcOrdersinbound.OrderResponse_element MIOA_SFDC_to_SAP_Order(String CreatedBy,String LastModifiedBy,String Owner,String RecordType,String Account_Owner_xc,String Business_Partner_xc,String Contact_Person_xc,String Delivery_date_xc,String Description_xc,String External_Reference_xc,Double Net_Value_xc,String Partner_Id_xc,String Sales_Organisation_xc,String Status_xc,String Visit_xc,String Wholesaler_xc,mmmmmSfdcOrdersinbound.Order_Line_Item_xc[] Order_Line_Item_xc) {
            mmmmmSfdcOrdersinbound.OrderRequest_element request_x = new mmmmmSfdcOrdersinbound.OrderRequest_element();
            request_x.CreatedBy = CreatedBy;
            request_x.LastModifiedBy = LastModifiedBy;
            request_x.Owner = Owner;
             ....
            ......
            .......

            request_x.Order_Line_Item_xc = Order_Line_Item_xc;
            mmmmmSfdcOrdersinbound.OrderResponse_element response_x;
            Map<String, mmmmmSfdcOrdersinbound.OrderResponse_element> response_map_x = new Map<String, mmmmmSfdcOrdersinbound.OrderResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'urn:mmmmm:sfdc:ordersinbound',
              'OrderRequest',
              'urn:mmmmm:sfdc:ordersinbound',
              'OrderResponse',
              'mmmmmSfdcOrdersinbound.OrderResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x;
        }
    }
}
Can anyone please let me know as to how and where do i specify the credentials?
Or what adjustments do i need to make in the auto generated apex class?


Thanks
Best Answer chosen by Neha@Sfdc
BalajiRanganathanBalajiRanganathan
your base64 looks good. as i said before try generating the base64 online and use it.
1) Try to use Basic instead of BASIC
2) Are you passing all the inputs required for the webserice? try to test the webserice in SOAP UI. in Soap UI also you have to set the credentials.
3) enable the debug for the salesforce user which does the call out and check the debug logs for any other error.

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
I believe this should be executed in Salesforce user so you will not any auth for same. If at all you are trying to pass Salesforce creds for authenticating 3rd party service then you can use token generation or oAuth for authentication.
Neha@SfdcNeha@Sfdc
hello sumit,

I didn't really get as to what you explained.
I am trying to invoke an external webservice that requires credentials.
But i dont understand as to how do i pass in those credentials..when i acess/inoke the above auto generated apex class
BalajiRanganathanBalajiRanganathan
You need to add the Autherization header while calling the generated class

mmmmmSfdcOrdersinbound.Soap service = new mmmmmSfdcOrdersinbound.Soap();
service.inputHttpHeaders_x = new Map<String, String>();
service.inputHttpHeaders_x.put('Authorization', 'Basic cGFud2F1dGg6UEBudzQ0MDEh');

cGFud2F1dGg6UEBudzQ0MDEh, replace this with username:password encoded in base 64.

Refer "HTTP Header Support" on this link below.

https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

http://en.wikipedia.org/wiki/Basic_access_authentication
 
Neha@SfdcNeha@Sfdc
hi balaji,
thanka for your reply,

According to your suggestion my header would be something like this
// providing the cred inputs
           createOrder.inputHttpHeaders_x=new Map<String,String>();
           String username = 'XXXX';
           String password = 'YYYY';  
           Blob headerValue = Blob.valueOf(username + ':' + password);
           String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
           createOrder.inputHttpHeaders_x.put('Authorization',authorizationHeader);

           
          //invoking the webservice to send orders from Salesforce  
          callOutResponse=createOrder.MIOA_SFDC_to_SAP_Order(String input);

Is this correct?
Neha@SfdcNeha@Sfdc
After executing the above code i am still getting an error as:

00:03:42:737 FATAL_ERROR System.CalloutException: Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found ':html'

It seems to be redirecting to an error page that renders output in HTML.I assume the error is due to improper authentication.

Can anyone please suggest as to how can  i authencate successfully?
BalajiRanganathanBalajiRanganathan
Yes you got it correct. But If you are putting user name and password in your code itself, then you can generate the base64 online or using ananymous window and set the encoded value directly.
createOrder.inputHttpHeaders_x.put('Authorization','Basic <your base 64 of username:password>';
);
BalajiRanganathanBalajiRanganathan
You have to verify

1) If you have added the webserice domain name in remote site settings
2) If you have your endpoint correct.
3) If the web service host is allowing outside ip's like salesforce to have access. 

check your network team if your websercie can be accessed outside of your company network. the service need be accessed outside of the network and you can provide then salesforce ip ranges.

I beleive mostly your issue with no 3) as given above.

https://help.salesforce.com/apex/HTViewSolution?id=000003652
Neha@SfdcNeha@Sfdc
thanks for your assistance.
the endpoint is accessed publicly over the internet.

1.when i open the endpoint on browser,it asks fopr credentials,When i mention the credentials,it displays something with status ok

User-added image
2.Also now i get an error as

 Web service callout failed: Unexpected element. Parser was expecting element 'urn:mmmmmmm:sfdc:ordersinbound:OrderResponse' but found 'http://schemas.xmlsoap.org/soap/envelope/:Body'

The highlighted text shows a slight difference between the previous and the current error
 
Neha@SfdcNeha@Sfdc
can you please show here as to how do i mention base64 if what i have written is incorrect?

Thanks
BalajiRanganathanBalajiRanganathan
your base64 looks good. as i said before try generating the base64 online and use it.
1) Try to use Basic instead of BASIC
2) Are you passing all the inputs required for the webserice? try to test the webserice in SOAP UI. in Soap UI also you have to set the credentials.
3) enable the debug for the salesforce user which does the call out and check the debug logs for any other error.
This was selected as the best answer
Neha@SfdcNeha@Sfdc
ok but then how do i specify credentials on SOAP UI tool?
BalajiRanganathanBalajiRanganathan
below the SOAP Request window, you can see tab named Auth. click on that and follow the steps as in the link below
http://www.soapui.org/OAuth/add-authorization.html
Neha@SfdcNeha@Sfdc
i sont understand whats wrong but i am unable to find the AUTH tab.i am using soap ui 5.0.0
BalajiRanganathanBalajiRanganathan
it is on the window where you specify the soap request.
1) Once you created the SOAP project using wsdl, click on the request1
2) on the popup window, down below SOAP Request, you will see Auth,Headers, Atttachments etc. it is on the left down corner of the SOPA request window
Neha@SfdcNeha@Sfdc
thanks balaji..i was about to reply you that i got it..i'll try from there and get back to you
Neha@SfdcNeha@Sfdc
Hi balaji,

I tried to invoke the webservvice via SOAP UI tool and it shows an error as
Connection to http://.... refused.
What can e the issue?
i am trying very hard to get through this 
i have also provided proper credentials.
Also whenever i open up the endpoint directly via my browser it prompts for credentials.
Thereafter the page displays an message as i had mentioned in the screenshot in my earlier comment.
Is something wrong in my code itself?or its network related problem?

Thanks
BalajiRanganathanBalajiRanganathan
try using https:// and if it is still not working check with your network team
Neha@SfdcNeha@Sfdc
thanks but unfortunately its still not working...
When i try to invoke the webservice from apex class it shows up as this:

System.CalloutException: Web service callout failed: Unexpected element. Parser was expecting element 'urn:mmmmmm:sfdc:ordersinbound:OrderResponse' but found 'http://schemas.xmlsoap.org/soap/envelope/:Body'
 
BalajiRanganathanBalajiRanganathan
1) check the port number on the endpoint
2) as i said before, check if you are passing all the required inputs
3) is it public webservice or hosted in your network? if it is in your network, then check if it is accessible from Salesforce.
   you can access it using your browser does not mean that it is public. you are on the company network while accessing your browser
   but sfdc does not call your service from your network.
4) try to test the service in soap UI.

if nothing is working, then submit a new thread so that other people might able to help you.
Neha@SfdcNeha@Sfdc
thanks balaji...for your continued assistance.

Now the request is placed successfully in SAP but still an error pops up in sfdc the reason being that they are not sending any response once they send that too will be fixed..

Once again i appreciate all your efforts