1

I tried to use the logic of this curl command:

curl -s --insecure -i --header ${AUTH_BASIC} --header ${CONTENT_TYPE} -X POST https://idm/oauth2/token -d ${DATA}"
    XAUTH_TOKEN="$(eval ${REQUEST} | grep -Po '(?<="access_token": ")[^"]*')"
    echo "X-Auth-Token for '${_user}': ${XAUTH_TOKEN}

to write the request in c#:

//GETTING TOKEN...
            String input2 = "'grant_type=password&username=<MyUsername on Lab .fiware.org&password=<myPassword>&client_id=<myClientID>&client_secret=<myClientSecret>'";
            var httpWebRequest2 = (HttpWebRequest)WebRequest.Create("https://account.lab.fiware.org/oauth2/token");
            httpWebRequest2.ContentType = "application/x-www-form-urlencoded";
            //httpWebRequest2.Accept = "application/json";
            string authInfo = "0555996e09f340d08a4baa8fff94f8c4" + ":" + "a62333f7045b4ab797669c28f9d26d30";
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            httpWebRequest2.Headers["Authorization"] = "Basic " + authInfo;
            httpWebRequest2.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest2.GetRequestStream()))
            {
                streamWriter.Write(input2);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse();
            using (var streamReader = new StreamReader(httpResponse2.GetResponseStream()))
            {
                string result = streamReader.ReadToEnd();
                Console.WriteLine(result);
            }

But I get the following error:

enter image description here

When I tried doing this solution in localhost, like in the post here it works without a problem. Could it have anything to do with the fact that I registered the app under localhost:1307 in the lab account?

Community
  • 1
  • 1
Vrankela
  • 1,162
  • 3
  • 16
  • 39

1 Answers1

2

There are not enough details to debug it, but KeyRock returns 400 when the request it's not well formed. You should get the message KeyRock returns. Still, with this request, you can get code 400 if:

  • The Authorization: Basic header is missing, in which case you will get:

    HTTP/1.1 400 BAD REQUEST
    Date: Thu, 10 Sep 2015 08:43:25 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Vary: Accept-Language,Cookie
    X-Frame-Options: SAMEORIGIN
    Content-Language: en
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8
    
    Authentication header missing. Use HTTP Basic.
    
  • The request body (i.e. your input2) is not being sent, in which case you will get:

    HTTP/1.1 400 Bad Request
    Date: Thu, 10 Sep 2015 08:47:49 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Vary: Accept-Language,Cookie
    X-Frame-Options: SAMEORIGIN
    Content-Language: en
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: application/json
    
    {"error": {"message": "create_access_token() takes exactly 3 arguments (2 given)", "code": 400, "title": "Bad Request"}}
    
  • The grant_type is not defined in your request body:

    HTTP/1.1 400 Bad Request
    Date: Thu, 10 Sep 2015 08:52:58 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Vary: Accept-Language,Cookie
    X-Frame-Options: SAMEORIGIN
    Content-Language: en
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: application/json
    
    {"error": {"message": "grant_type missing in request body: {}", "code": 400, "title": "Bad Request"}}
    
albertinisg
  • 491
  • 4
  • 12
  • Very well, we will see what the stack-trace says. In the mean time do you by any chance know the length of the token session? We generated the token locally from the machine where pep proxy runs and just forward it to the machine from where we are running the C# code and it works. This has been over 24 hours ago since we generated the token and we are wondering when does it expire? – Vrankela Sep 10 '15 at 09:45
  • Length depends if you want the length of the token or the `Content-length`. By default token `"expires_in":3600` – albertinisg Sep 10 '15 at 09:55
  • I mean when it expires. I didnt touch any configurations when the tokens expire, that means that I shouldn't be able to use the same token after an hour? And yet here I am using tokens that were generated more than 24 hours ago. to the original question, we are having trouble generating the error to diagnose the problem, we can only generate: HTTP/1.1 400 Bad Request Date: Thu, 10 Sep 2015 08:52:58 GMT Server: Apache/2.4.7 (Ubuntu) Vary: Accept-Language,Cookie X-Frame-Options: SAMEORIGIN Content-Language: en Connection: close Transfer-Encoding: chunked Content-Type: application/json – Vrankela Sep 10 '15 at 10:02
  • Then the expiration time is the one given below. Regarding the request, what you've pasted are the response headers but not the body. The body is the one that determines the error, (as you can see, a JSON) as it says in the `Content-type`; so then it should be the second or third option I've explained in my answer. – albertinisg Sep 10 '15 at 11:37
  • I found the error body, its the third one "grant_type missing". Can you suggest a solution? As you can see, we are passing the grant type in the head of the request. – Vrankela Sep 10 '15 at 14:43
  • You are using both double and single quotes to declare the string `input2`, and I'm almost sure this is causing an issue, because you are sending the data with quotes. – albertinisg Sep 10 '15 at 15:19