[英]Basic HTTP Authentication from Salesforce to Taleo
I am a newbie and am writing a SOAP web service (for integration purpose), in order to execute the SOAP call I need to authenticate the user first(standard integration user). 我是新手,并且正在编写SOAP Web服务(出于集成目的),为了执行SOAP调用,我需要首先验证用户(标准集成用户)。
Following is the code snippet for it. 以下是其代码段。 However, when I execute the callout, it throws error code 500 for Basic Http request and error code 401 for the second Http request.
但是,当我执行标注时,它将为基本Http请求抛出错误代码500,为第二个Http请求抛出错误代码401。
Is this the correct approach? 这是正确的方法吗?
HTTP auth = new HTTP();
HTTPRequest r = new HTTPRequest();
r.setEndpoint('https://domainname.net/enterprise/soap?ServiceName=IntegrationManagementService');
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
r.setMethod('POST');
try
{
HTTPResponse authresp = auth.send(r);
if(authresp.getStatusCode() == 200)
{
system.debug('Authentication success!!!' + authresp);
}
else
{system.debug('Authentication failed!!!' + authresp + authresp.getStatusCode());}
}catch(exception e){}
//construct http request
string endpointURL = 'https://doaminname.net/enterprise/soap?ServiceName=IntegrationManagementService';
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(endpointURL);
req.setHeader('Content-Type','application/xml');
req.setBody(TaleoXML);
//send http request
Http http = new Http();
try
{
HttpResponse res = http.send(req);
//check the response
if(res.getStatusCode() == 200)
{
system.debug('Callout success!!!' + res);
}
else
{system.debug('Callout failed!!!' + res + res.getStatusCode());}
}catch(exception e){}
I'm not familiar with the library you're using here, but I can suggest a few possibilities to investigate: 我不熟悉您在此处使用的库,但我可以建议您进行以下几种研究:
Basic authentication is a stateless method. 基本身份验证是一种无状态方法。 You don't log in and stay logged in with it.
您无需登录并保持登录状态。 This means there's no separate initial authentication request (as your code implies);
这意味着没有单独的初始身份验证请求(如您的代码所示); you include the
Authorization
header with every request. 您在每个请求中都包含了
Authorization
标头。 That's why you're getting the 401 on the second request. 这就是为什么您在第二个请求中得到401的原因。
On the first request, you are providing credentials and the the server is encountering an unexpected internal error (that's what 500 means). 在第一个请求上,您正在提供凭据,并且服务器遇到意外的内部错误(这就是500的含义)。 If it includes a body with the error response, it might have more information.
如果它包含具有错误响应的正文,则它可能具有更多信息。 I would guess that it has something to do with the fact that you didn't provide a body with your POST and the server was not expecting that.
我猜想这与以下事实有关:您没有为POST提供主体,并且服务器没有期望这一点。
If this is the first time you're using SOAP, you would probably be better off using a dedicated SOAP library rather than trying to construct requests yourself. 如果这是您第一次使用SOAP,则最好使用专用的SOAP库,而不是尝试自己构造请求。
In the second request you don't include Authentication, that's why you get a 401 (Unauthorized) error. 在第二个请求中,您不包括身份验证,这就是为什么您会收到401(未经授权)错误的原因。
In the first request, it seems you authenticate ok but the server fails to process the request. 在第一个请求中,您似乎已通过身份验证,但服务器无法处理该请求。 I think you missed to refer to the function/operation of the IntegrationManagementService webservice that you want to use.
我认为您错过了要使用的IntegrationManagementService Web服务的功能/操作。 Or you are using a function/operation that needs MTOM enabled.
或者您正在使用需要启用MTOM的功能/操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.