0

NiFi の InvokeHTTP プロセッサを使用して、SalesForce エンドポイント、特に Login エンドポイントに POST します。

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>

さらに、InvokeHTTP プロセッサを通過する JSON メッセージ/ペイロードがあるので、構成します

Content-Type: application/json

そして、これを実行するとうまくいきます。

[注: Apache NiFi を知らないが、Java の HttpClient や SFDC を知っている人は質問に答えることができます。要点は、REST API エンドポイントは NiFi では機能しますが、REST API にアクセスしようとすると機能しないということです。カスタム 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"}

また、パラメーターが私のエンティティになるため、JSON ペイロードを使用せずに、ページで項目 #2 のパターンも試しました。

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両方のパターンを withおよびjsonPayloadas エンティティと組み合わせるにはどうすればよいですか?

4

1 に答える 1