0

我正在使用 NiFi 的 InvokeHTTP 处理器 POST 到 SalesForce 端点,特别是到 Login 端点:

https://test.salesforce.com/services/oauth2/token

我将 client_id、client_secret、用户名和密码字段附加到 URL:

https://test.salesforce.com/services/oauth2/token?grant_type=password&client_id=<client_id>&client_secret=<client_secret>&username=<username>&password=<password + key>

此外,还有一个 JSON 消息/有效负载通过 InvokeHTTP 处理器,所以我配置

Content-Type: application/json

当我运行它时,它工作正常。

[注意:那些不知道 Apache NiFi 但知道 Java 中的 HttpClient 和/或 SFDC 的人可以回答这个问题,我的观点是 REST API 端点在 NiFi 上对我有效,但当我尝试使用自定义Java代码]

现在因为我想将此机制转换为 ExecuteScript 处理器中的自定义代码,所以我尝试使用 HttpClient 库在 Java 中进行编码。但根据Baeldung中的这篇文章,似乎有多种方法可以做到这一点。我首先尝试了第 4 项:

CloseableHttpClient client = HttpClients.createDefault();

String urlStr = "https://test.salesforce.com/services/oauth2/token?grant_type=password&client_id=<client_id>&client_secret=<client_secret>&username=<username>&password=<password + key>";
HttpPost httpPost = new HttpPost(urlStr);

String jsonPayload = "{\"qty\":100,\"name\":\"iPad 4\"}";
StringEntity entity = new StringEntity(jsonPayload);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

CloseableHttpResponse response = client.execute(httpPost);

System.out.println(response.getStatusLine().getStatusCode());

InputStream is = response.getEntity().getContent();
String responseStr = IOUtils.toString(is, "UTF-8");

System.out.println(responseStr);

client.close();

我得到:

400
{"error":"invalid_grant","error_description":"authentication failure"}

我还在页面中尝试了第 2 项模式,没有 JSON 有效负载,因为参数现在将是我的实体:

CloseableHttpClient client = HttpClients.createDefault();

String urlStr = "https://test.salesforce.com/services/oauth2/token";

HttpPost httpPost = new HttpPost(urlStr);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", CLIENT_ID));
params.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
params.add(new BasicNameValuePair("username", USERNAME));
params.add(new BasicNameValuePair("password", PASSWORD));

httpPost.setEntity(new UrlEncodedFormEntity(params));

CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());

InputStream is = response.getEntity().getContent();
String responseStr = IOUtils.toString(is, "UTF-8");

System.out.println(responseStr);

client.close();

在这种情况下,我也会得到相同的响应。我的 Java 代码中是否缺少某些内容?如何将两种模式与params实体jsonPayload结合起来?

4

1 回答 1

0

这是我对HTTPClient的经验,对于Bing search API

      URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/search");

        builder.setParameter("q", line);
        builder.setParameter("count", "10");
        builder.setParameter("offset", "0");
        builder.setParameter("mkt", "en-us");
        builder.setParameter("safesearch", "Moderate");

        URI uri = builder.build();
        HttpGet request = new HttpGet(uri);

        request.setHeader(api key);
        HttpClient httpclient = HttpClients.createDefault();
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

稍后,您需要根据需要阅读entity和操作它。

于 2017-06-07T05:45:03.343 回答