Problems getting Oauth token for sharepoint online using POST request node

Hi.
I’m trying to get an authorization token for sharepoint online using POST request node. I read all similar topics but they didn’t help me.

The problem is that knime somehow is unable to send correct data in the POST request body and i get the error - Execute failed: Wrong status: 400 Bad Request

The same request is working fine in Postman and even in knime using python script.
I added images of the Post request setup (i made the user codes on the body tab unsusable, i hope the structure is enough:


And in knime log there is only this:
2020-05-27 08:12:38,632 : ERROR : KNIME-Worker-21-POST Request 0:296 : : Node : POST Request : 0:296 : Execute failed: Wrong status: 400 Bad Request
java.lang.IllegalStateException: Wrong status: 400 Bad Request
at org.knime.rest.nodes.common.RestNodeModel.checkResponse(RestNodeModel.java:793)
at org.knime.rest.nodes.common.RestNodeModel.makeFirstCall(RestNodeModel.java:406)
at org.knime.rest.nodes.common.RestNodeModel.execute(RestNodeModel.java:340)
at org.knime.core.node.NodeModel.execute(NodeModel.java:737)
at org.knime.core.node.NodeModel.executeModel(NodeModel.java:571)
at org.knime.core.node.Node.invokeFullyNodeModelExecute(Node.java:1236)
at org.knime.core.node.Node.execute(Node.java:1016)
at org.knime.core.node.workflow.NativeNodeContainer.performExecuteNode(NativeNodeContainer.java:557)
at org.knime.core.node.exec.LocalNodeExecutionJob.mainExecute(LocalNodeExecutionJob.java:95)
at org.knime.core.node.workflow.NodeExecutionJob.internalRun(NodeExecutionJob.java:218)
at org.knime.core.node.workflow.NodeExecutionJob.run(NodeExecutionJob.java:124)
at org.knime.core.util.ThreadUtils$RunnableWithContextImpl.runWithContext(ThreadUtils.java:334)
at org.knime.core.util.ThreadUtils$RunnableWithContext.run(ThreadUtils.java:210)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.knime.core.util.ThreadPool$MyFuture.run(ThreadPool.java:123)
at org.knime.core.util.ThreadPool$Worker.run(ThreadPool.java:246)

The problem is that i don’t understand why it fails. Are there some symbols that are not recognized by java? the authorization codes contain both “=” and “/” as part of them. Do i have to escape them somehow?
I also read that i might use java snippet node to make the api call but that requires me to search the web for examples.

1 Like

Hi,
this looks like a application/x-www-form-urlencoded body. Did you set the Content-Type header accordingly? Additionally, the each value of the key-value pairs separated by “&” must be encoded, e.g. the forward slash becomes “%2F”. For this you can use the Column Expressions node with the encodeURIComponent function. If you want to pass in the whole body, you can use something like this:

column("bodycolumn").split("&")
.map(function(kv) {
    var s = kv.split("=");
    return s[0] + "=" + encodeURIComponent(s[1]);
})
.join("&")

Be aware that the key-value pairs must not contain ampersands or equal signs themselves.
Kind regards,
Alexander

Hi thanks for the advice and the new node. Sadly this does not help, i still get the 400 bad request error.
I do have the correct header - see a few images above - i posted all configuration.

Hi,
maybe that is not the cause, since you say that in Python and Postman the same request works, but in this description they write that the client_id in the body should look like CLIENT_ID@TENANT_ID. In your request the tenant id is not included here. Could that be it? Can you also post the encoded body?
Kind regards,
Alexander

As i said - i corrupted it so others cant use it. The format is right since it works in 2 other cases. ’
I was asking for possible bug. Maybe the internal Java code somehow can’t create a correct request.

Hi,
My idea was that maybe Python and Postman automatically encode the string correctly, while in Java you have to do it manually before setting the body. Does the same problem occur with different endpoints, or just the one?
Kind regards
Alexander