# API Call with "Authorization: Token"

**URL:** <https://forum.knime.com/t/api-call-with-authorization-token/8001>\
**Category:** KNIME Extensions\
**Tags:** rest\
**Created:** [May 20, 2015, 12:45pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001 "2015-05-20T12:45:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonnyblacklabel](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@Jonnyblacklabel](https://forum.knime.com/u/Jonnyblacklabel)\
**Post date:** [May 20, 2015, 12:45pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/1 "2015-05-20T12:45:24Z")

</div>

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.

&nbsp;

Is it possible to make that API call? Thanks for your Help.

J.

---

<div class="post-metadata">

**Author:** ![lborisov](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/l/a3d4f5/32.png) [@lborisov](https://forum.knime.com/u/lborisov)\
**Post date:** [May 20, 2015, 5:18pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/2 "2015-05-20T17:18:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jonnyblacklabel](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@Jonnyblacklabel](https://forum.knime.com/u/Jonnyblacklabel)\
**Post date:** [May 21, 2015, 9:10am UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/3 "2015-05-21T09:10:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lborisov](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/l/a3d4f5/32.png) [@lborisov](https://forum.knime.com/u/lborisov)\
**Post date:** [May 21, 2015, 12:59pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/4 "2015-05-21T12:59:18Z")

</div>

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
    }
}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![changoman](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@changoman](https://forum.knime.com/u/changoman)\
**Post date:** [July 21, 2016, 4:04am UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/5 "2016-07-21T04:04:10Z")

</div>

Hi all.

Any update about this topic?

Thanks!

---

<div class="post-metadata">

**Author:** ![ferry.abt](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/f/e5b9ba/32.png) [@ferry.abt](https://forum.knime.com/u/ferry.abt)\
**Post date:** [August 9, 2016, 3:57pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/6 "2016-08-09T15:57:54Z")

</div>

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)&nbsp;possible. Please have a look and tell me if it does what you need.

Best,  
Ferry

---

<div class="post-metadata">

**Author:** ![anguslou](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@anguslou](https://forum.knime.com/u/anguslou)\
**Post date:** [November 9, 2021, 9:41am UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/7 "2021-11-09T09:41:25Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![bruno29a](https://forum-cdn.knime.com/user_avatar/forum.knime.com/bruno29a/32/28802_2.png) [@bruno29a](https://forum.knime.com/u/bruno29a)\
**Post date:** [November 9, 2021, 3:23pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/8 "2021-11-09T15:23:07Z")

</div>

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:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/f/4/f45aa79df2cfe2336b94c75f258643ca317bc5ff.png)

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:  
 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/b/3/b32dbb646345ec4297fc0c175f2c0628192d2347.png)

You should see the header values have been added:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/1/9/195d48a4d41153771bfcb0f6b887e0b862e3959a.png)

---

<div class="post-metadata">

**Author:** ![anguslou](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@anguslou](https://forum.knime.com/u/anguslou)\
**Post date:** [November 11, 2021, 10:33am UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/9 "2021-11-11T10:33:01Z")

</div>

Thank you. That’s the solution.

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [June 2, 2023, 9:09pm UTC](https://forum.knime.com/t/api-call-with-authorization-token/8001/10 "2023-06-02T21:09:08Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
