2

I am having a big problem when trying to authenticate to QuickBlox's server using a Token.

The method I use is:

public static async Task<LoginResponse> GetLoginResponseAsync(string email, string password)
{
    LoginResponse result = null;

    using (var client = new HttpClient())
    {
        string token = QbProvider.SessionResponse.Session.Token;

        LoginRequest request = new LoginRequest()
        {
            Email = email,
            Password = password
        };

        using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, LoginUrlRequest))
        {
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("QB-Token", token);

            using (var response = await client.SendAsync(requestMessage))
            {
                string json = response.Content.ReadAsStringAsync().Result;
                result = JsonConvert.DeserializeObject<LoginResponse>(json);
            }
        }
    }

    return result;
}

The server's response is:

{"errors":["Token is required"]}

And the headers (debugging) in the client object are:

{
    StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
        Access-Control-Allow-Origin: *
        Cache-Control: no-cache
        Date: Thu, 01 Jun 2017 12:05:29 GMT
        QuickBlox-REST-API-Version: 0.1.1
        Server: openresty/1.9.15.1
        Status: 401 Unauthorized
        Strict-Transport-Security: max-age=31536000
        WWW-Authenticate: OAuth realm=users
        X-Content-Type-Options: nosniff
        X-Frame-Options: SAMEORIGIN
        X-Request-Id: 584a0dca-fc44-4114-9626-327ac1729f67
        X-Runtime: 0.003430
        X-XSS-Protection: 1; mode=block
        Connection: keep-alive
        Content-Type: application/json; charset=utf-8
        Content-Length: 32
    }
}

When I use the Token in Postman, the server's response is successfull.

Do you know what am I doing wrong?

Thank you so much in advance! Regards.

  • Did you compare the requests against each other - Postman versus your code. You can use tools like [Fiddler](https://www.telerik.com/download/fiddler/fiddler2) to compare the requests. – Gururaj Jun 01 '17 at 12:23
  • Can you confirm if the token is taken from the session variable – Saravanan Jun 01 '17 at 12:24
  • As per the documents, i think you have to use the User's session token https://quickblox.com/developers/Authentication_and_Authorization#Requests_and_Responses – Saravanan Jun 01 '17 at 12:26
  • Try adding your token header using `requestMessage.Headers.Add("QB-Token", token)` instead of your `Authorization` one – dukedukes Jun 01 '17 at 12:30
  • The token is taken by the session variable. – Àlex Martínez Jun 01 '17 at 14:11
  • @dukedukes I have added that line and now there is a different error: {"errors":{"login":["(or email) cannot be blank"],"password":["cannot be blank"]}} I have checked in debugging mode that the JSON sent have the email and password. – Àlex Martínez Jun 01 '17 at 14:14
  • Ok now it works! Thank you so much @dukedukes. I forgot to send the request object to the POST. – Àlex Martínez Jun 01 '17 at 14:20

1 Answers1

0

Try adding your token header using requestMessage.Headers.Add("QB-Token", token) instead of your Authorization one – by @dukedukes