c# - 对 REST API 的未授权访问

标签 c# rest xamarin quickblox

我在尝试使用 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;
}

服务器的响应是:

{"错误":["需要 token "]}

客户端对象中的 header (调试)是:

{
    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时,服务器响应成功。

你知道我做错了什么吗?

非常感谢您! 问候。

最佳答案

尝试使用 requestMessage.Headers.Add("QB-Token", token) 而不是您的授权来添加您的 token header – @dukedukes

关于c# - 对 REST API 的未授权访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44307349/

相关文章:

c# - DependencyResolver.SetResolver 不工作

java - Spring Rest PostMapping 从 json 映射到多个表

Xamarin 在 ScrollView 中形成 ListView 问题

android - Android内存转储中的EGL和GL mtrack是什么

c# - 为什么逗号在具有 InvariantCulture 的 int.Parse() 和 decimal.Parse() 中表现不同?

c# - 访问窗口 UWP C# 上的关闭按钮

c# - ASP.Net Web API 模型构造函数绑定(bind) - 调用了错误的构造函数

database - rest:修改与资源关联的资源的顺序

java - JAX-RS Web 服务在哪个容器中运行?

c# - 从 Azure Pipeline 生成 Xamarin-iOS nuget 时出现奇怪的 'Native linking failed, undefined Objective-C class'(但在本地构建时则不然)