Hi,
I try to implement a HTTP REST API driver to monitor some equipment from a website which requires some authentication. The provider provides some detail example on curl and Postman which works fine. But how to implement the authentication parameter in the HTTP session? Here follow the curl request (I just replaced the 'client_id & _secret by dummy value):
curl --location --request POST 'https://api-knt.skylogic.com/ext/api/v1/whs/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=xxxxxx' \
--data-urlencode 'client_secret=yyyyyy'
And so far my HTTP session looks like that in Dataminer:
<HTTP>
<Session id="1" name="Get Token">
<Connection id="1" name="Get Token">
<Request verb="POST" url="/ext/api/v1/whs/oauth2/token">
<Headers>
<Header key="Content-Type">application/x-www-form-urlencoded</Header>
</Headers>
</Request>
<Response statusCode="100">
<Content pid="200"/>
</Response>
</Connection>
</Session>
</HTTP>
It is triggered by a button and the server is sending back some error info message in JSON (Err. 500) as the authentication is not yet implemented. At least it proves the request is sent but I am stuck to how to "translate" the full curl request in the driver:
{"timestamp":1675335822712,"status":500,"error":"Internal Server Error","path":"/api/v1/whs/oauth2/token","id":"051334ef-9c53-49e8-a131-15e061ae5e7c.","message":null,"trace_id":"ebff834954eff057","span_id":"d41a198b1a4737c4"}
Hi,
The parameters grant_type, client_id, client secret will still need to be included.
That can be done on 2 possible ways:
- A parameter will have as value the full content of the post body. See this help section
- Or specify them as parameter format, for more info see this help section (for example <Parameter key="grant_type">client_credentials</Parameter> <Parameter key="client_id" pid="215" />, for grant_type this is a fixed value, for client_id the value will be stored in parameter with id 215
Note when using a GET that it will add the parameters in the URL already url encoded (with ampersand between every item). For a POST this will add it to the body. I'm not 100% sure it will be url encoded in that case, it could be that it will still needs to be some qaction logic in between before filling in the parameter value (that would need to be tested out)
Hi Dominique, I’m glad to hear that the info could help. If the provided answer is sufficient, would it be possible to click on the check mark beneath the upvote/downvote icons (has the tooltip “Select this answer as best.”)? This way the question will be marked as solved
I’ve done it Laurens – and you can do that as well as a matter of fact, if a question is solved anybody is welcome to mark it as solved. Because indeed it is good to keep a bit of hygiene on these things.
PS: the feedback you gave, is that also documented somewhere in docs.dataminer.services for future reference?
Small correction: Only the person asking the question, or an admin or moderator, can mark an answer as best answer. So, you are probably an admin or moderator. Laurens probably not.
I can only mark an answer as best answer if I’m the one that posted the question. If somebody else posted a question then I cannot mark something as best answer.
About the feedback that I gave: I did some further lookup on docs.dataminer.services and found a nice explanation with examples about the usage of “Data” and “Parameter” when used with a “GET” or a “POST” in this link: https://docs.dataminer.services/develop/devguide/Connector/ConnectionsHttpImplementing.html#request
Many thanks Laurens, very helpful 🙂
So I implemented the credentials as you advised and it works fine for that POST:
client_credentials
application/x-www-form-urlencoded
In fact this POST generates a token that I will have to extract with a QAction to be able to use other commands. Once again, thank you very much