java - Force.com Apex 代码对应部分或 Java 的等效代码

标签 java salesforce apex-code apex force.com

我不确定我是否走在正确的道路上。因为我没有通过 HTTP 请求获取任何数据。我需要将此 java 代码转换为 force.com apex,以便能够使用 WTS Web 服务。

public static HttpEntity getRequestEntity()
{
  JSONArray jsonArr = new JSONArray();
  HttpEntity entity = null;
  JSONObject jsonElement = new JSONObject();
  JSONObject jsonObj = new JSONObject();
  JSONObject jsonObj1 = new JSONObject();
  jsonElement.put("tenant",jsonObj);
  jsonObj.put("companyKey",companyName);

  String json = jsonElement.toJSONString();
  try {
    entity = new StringEntity(json);
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  }
  return entity;
}

private static void GetEnabledUsers(String responseContent) throws ParseException, ClientProtocolException, IOException {
String url  = "<endpoint>/services/UserService1.svc/GetEnabledUsers";

// Create an http client to perform the http communication
CloseableHttpClient httpClient = HttpClients.createDefault();

// Http request to specific web service. Request will be given to client
HttpPost request =  new HttpPost(url);

// Create credentials for the authentication
CredentialsProvider provider =  new BasicCredentialsProvider();

UsernamePasswordCredentials credential = new UsernamePasswordCredentials(companyName+'\\'+userName, password);
provider.setCredentials(AuthScope.ANY, credential);
HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(provider);

// Make the actual http request
HttpPost httpRequest =  new HttpPost(url);

HttpEntity requestEntity = getRequestEntity();
if(requestEntity != null) {
httpRequest.setHeader("Content-type", "application/json");
httpRequest.setEntity(requestEntity);
}

CloseableHttpResponse response = httpClient.execute(httpRequest, localContext);
HttpEntity entity = response.getEntity();
String responseContent1 = EntityUtils.toString(entity);
System.out.println(responseContent1);
}

这是我到目前为止所拥有的:

String userName= '<userName>';
String password= '<password>';
String companeyKey = '<companeyKey>';

JSONGenerator jsonGenerator = JSON.createGenerator(true);
jsonGenerator.writeStartObject();
  jsonGenerator.writeFieldName('tenant');
    jsonGenerator.writeStartObject();
      jsonGenerator.writeStringField('companeyKey', '<companeyKey>');
    jsonGenerator.writeEndObject();
jsonGenerator.writeEndObject();

requestBody = jsonGenerator.getAsString();

Blob headerValue = Blob.valueOf(userName + ':' + password);
HttpRequest requestEntity = new HttpRequest();
String authHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

requestEntity.setMethod('POST');
requestEntity.setEndPoint('<endpoint>/services/UserService1.svc/GetEnabledUsers');
requestEntity.setHeader('Content-Type', 'application/json');
requestEntity.setHeader('Authorization', authHeader);
requestEntity.setBody(requestBody); 

Http http = new Http();
HTTPResponse responseEntity = http.send(requestEntity);

system.debug(responseEntity);

最佳答案

差异似乎在于如何为 auth 参数构造用户名,请更改

Blob headerValue = Blob.valueOf(用户名 + ':' + 密码);

Blob headerValue = Blob.valueOf(companyKey + '\\' + 用户名 + ':' + 密码);

关于java - Force.com Apex 代码对应部分或 Java 的等效代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237107/

相关文章:

java - 在 LibGDX 中加载图像

javascript - Uncaught ReferenceError : <mapkey> is not defined

javascript - Salesforce Commerce Cloud 使用哪个版本的 Rhino JavaScript?

VisualForce 页面中的 Javascript - 表单提交成功,触发顶点?

java - Android 应用程序跳帧,一段时间后崩溃

java - 在android中按下ok后重定向

web-services - SFDC 顶点代码 : Access class level static variable from "Future" method

linux - Linux 环境中的 Apex 数据加载器?

java - 需要根据从 JList 中选择的项目设置 double 值

salesforce - 未找到元素 : userDetails 的顶点类型