Hi Team, I am getting 401 error while trying to ge...
# troubleshoot
m
Hi Team, I am getting 401 error while trying to get the graphQL output through the python requests.post method. Is there a particular way that I had to authenticate the log in to datahub and then to execute the requests.post to get the response?
o
Have you included your generated bearer token from the UI as a header on your request?
m
Thanks for the suggestion to use the token generated from UI. I generated a token valid for 1 day and using in below code. However, I am still not getting authenticated but getting 401 status code. Could you please help on what I am missing here. resp=requests.post('<datahubURL>/api/graphql' ,headers={'Content-Type': 'application/json', 'Authorization': 'Bearer ' + <token>} ,verify=r'<path>') print(resp.status_code) print(resp.text) However, when I use the below code with query in data, I am getting 400 "Bad Request" resp=requests.post('<datahubURL>/api/graphql' ,headers={'Content-Type': 'application/json', 'Authorization': 'Bearer ' + <token>} ,data={"query":{"{me{ corpUser { username }}}"}} ,verify=r'<path>') print(resp.status_code) print(resp.text)
o
Looks like your JSON is wrong in the above request, should be:
Copy code
{
  "query": "{ me {  corpUser {    username  }}}"
}
as to why it still gives a 401 rather than a bad request without the data, I'm not totally sure. I can't reproduce that unfortunately. Can you share the logs for the 401?
m
I have used the above query mentioned , but still got the below error 400.
Copy code
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bad Request</title>
        <style>
            html, body, pre {
                margin: 0;
                padding: 0;
                font-family: Monaco, 'Lucida Console', monospace;
                background: #ECECEC;
            }
            h1 {
                margin: 0;
                background: #AD632A;
                padding: 20px 45px;
                color: #fff;
                text-shadow: 1px 1px 1px rgba(0,0,0,.3);
                border-bottom: 1px solid #9F5805;
                font-size: 28px;
            }
            p#detail {
                margin: 0;
                padding: 15px 45px;
                background: #F6A960;
                border-top: 4px solid #D29052;
                color: #733512;
                text-shadow: 1px 1px 1px rgba(255,255,255,.3);
                font-size: 14px;
                border-bottom: 1px solid #BA7F5B;
            }
        </style>
    </head>
    <body>
        <h1>Bad Request</h1>

        <p id="detail">
            For request 'POST /api/graphql' [Error decoding json body: com.fasterxml.jackson.core.JsonParseException: Unrecognized token &#x27;query&#x27;: was expecting (JSON String, Number, Array, Object or token &#x27;null&#x27;, &#x27;true&#x27; or &#x27;false&#x27;)
 at [Source: (akka.util.ByteIterator$ByteArrayIterator$$anon$1); line: 1, column: 7]]
        </p>

    </body>
</html>
a
Hey were u able to resolve this issue
r
Hey there! đź‘‹ Make sure your message includes the following information if relevant, so we can help more effectively! 1. Which DataHub version are you using? (e.g. 0.12.0) 2. Please post any relevant error logs on the thread!
o
This post is from 2 years ago, if you are having a similar issue please make your own new thread with details. You can link this issue in it as similar to your problem. If you are having the same problem as this thread, it is a JSON formatting error and your request needs to be fixed. You can see examples of what a correct request looks like by going through the UI and using Chrome’s inspector tool if you’re having trouble crafting it yourself.