Hey,
i need to make an API call and want to use KNIME an the krest nodes.
The curl command for the API would look like this:
curl -X POST [API-URL] -H "Authorization: Token 9b04a0df12c…fa6eff906d598af" -H "Content-Type: application/json" -d '{……}'
The Problem is, that i don't know how to represent this type of authorization with the rest node.
Is it possible to make that API call? Thanks for your Help.
J.
I faced exactly the same problem with authenticating against my application - KREST lacks support for Authorization header. I ended up writing code in Java snippet to get through authentication process and receive auth cookie in response.
Ok, i was afraid that it is just not possible. I even tried to find this Krest2 version, but it doesn't seem to be located in the Nightly Build URL.
Since i'm pretty new to java, is it possible that you send me your java snippet node, or the java code?
Thanks!
here it goes - snippet gets two params (c_username, c_password) and returns flow variable (out_token) with token/cookie:
// system imports
import org.knime.base.node.jsnippet.expression.AbstractJSnippet;
import org.knime.base.node.jsnippet.expression.Abort;
import org.knime.base.node.jsnippet.expression.Cell;
import org.knime.base.node.jsnippet.expression.ColumnException;
import org.knime.base.node.jsnippet.expression.TypeException;
import static org.knime.base.node.jsnippet.expression.Type.*;
import java.util.Date;
import java.util.Calendar;
import org.w3c.dom.Document;
// Your custom imports:
import javax.xml.bind.DatatypeConverter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.io.PrintStream;
import org.knime.core.node.NodeLogger;
// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for input columns
/** Input column: "username" */
public String c_username;
/** Input column: "password" */
public String c_password;
/** Input column: "auth_url" */
public String c_auth_url;
// Fields for output flow variables
/** Output flow variable: "toLWSSO" */
public String out_toLWSSO;
// Your custom variables:
// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
String enc_auth = DatatypeConverter.printBase64Binary(String.format("%s:%s", c_username, c_password).getBytes());
try{
// Open connection
HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(c_auth_url).openConnection();
// Clean Accept
httpUrlConnection.setRequestProperty("Accept", "text/plain");
Map<String,String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + enc_auth);
// Add HTTP headers
if (!headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpUrlConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
// Set HTTP method
httpUrlConnection.setRequestMethod("GET");
// Set response statusCode
httpUrlConnection.getResponseCode();
Map<String, List<String>> responseHeaders = httpUrlConnection.getHeaderFields();
List<String> allrespHeaders = responseHeaders.get("Set-Cookie");
NodeLogger LOGGER = NodeLogger.getLogger(AbstractJSnippet.class);
for(int i=0; i<allrespHeaders.size();i++){
String str = allrespHeaders.get(i);
if(str.contains("COOKIE_KEY")){
out_token = str;
LOGGER.warn("Found: " + str);
}
}
}
catch (Exception ex)
{}
// expression end
}
}
1 Like
Hi all.
Any update about this topic?
Thanks!
Hello,
Since May 2015 a lot has changed in the KNIME Analytics Platform, including the REST-services. As far as I know authentication is now (v3.2) possible. Please have a look and tell me if it does what you need.
Best,
Ferry
Hi,
I have a similar problem and I am running on KNIME 4.4. My curl is as following but how to put it into GET request node and have token authorization properly setup?
curl --location --request GET ‘http://websiteabcdefg-env-3.eba-h99gmigd.us-east-1.webrequest.com/api/v1/segments/top-asins?parent_segment_id=32&source_id=0&start_price=100&end_price=200&num_asins=7 ’
–header ‘Authorization: token abcdefghijklmnopqretuvwxyz’
Can somebody please help
Hi @anguslou , anything you pass as --header parameter just needs to be passed as headers in the GET Request.
Go to the Request Headers tab, and click on Add header parameter:
The header string you are passing is basically in the format of key: value
, so “Authorization: token abcdefghijklmnopqretuvwxyz
” means that the key is “Authorization” and the value is “token abcdefghijklmnopqretuvwxyz”
In that case, just add these in the popup window and click OK:
You should see the header values have been added:
4 Likes
Thank you. That’s the solution.
system
Closed
June 2, 2023, 9:09pm
10
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.