REST API访问未授权

我在使用 Token 认证到 QuickBlox 服务器时遇到了一个大问题。

我使用的方法是:

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;
}

服务器的响应是:

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

而客户端对象的标头(调试)是:

{
    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
    }
}

当我在 Postman 中使用 Token 时,服务器的响应是成功的。

你知道我错在哪里吗?

非常感谢您的帮助! 敬礼。

点赞
用户7702615
用户7702615

尝试使用 requestMessage.Headers.Add("QB-Token", token) 添加您的令牌标头,而不是您的授权标头 - by @dukedukes

2017-06-01 16:46:51