【问题标题】:Howto access REST API methods without username and Password - but only with a token?如何在没有用户名和密码的情况下访问 REST API 方法 - 但只能使用令牌?
【发布时间】:2019-08-22 10:39:54
【问题描述】:

我正在尝试使用来自 c#/.net 的令牌访问/调用 REST API 中的方法,但我无法得到任何响应。我用谷歌搜索了很多 - 但没有任何成功:-(我是通过 REST API 调用方法的新手。

我有一个端点和一个令牌,需要用于与 REST API 进行通信。我需要通过这些方法在服务器上获取、发布、放置和删除数据

API 的输出是 JSON 格式。

也许这很简单 - 但我不知道该怎么做。

感谢任何帮助。

我尝试了以下解决方案 - 但没有成功:-(

        private static async void DoIt()
        {
            using (var stringContent = new StringContent("{ \"firstName\": \"Andy\" }", System.Text.Encoding.UTF8, "application/json"))
            using (var client = new HttpClient())
            {
                try
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);

                    // 1. Consume the POST command
                    var response = await client.PostAsync(endpoint, stringContent);
                    var result = await response.Content.ReadAsStringAsync();
                    //Console.WriteLine("Result from POST command: " + result);

                    // 2. Consume the GET command
                    response = await client.GetAsync(endpoint);
                    if (response.IsSuccessStatusCode)
                    {
                        var id = await response.Content.ReadAsStringAsync();
                        //Console.WriteLine("Result from GET command: " + result);
                    }
                }
                catch (Exception ex)
                {
                    //Console.ForegroundColor = ConsoleColor.Red;
                    //Console.WriteLine(ex.Message);
                    //Console.ResetColor();
                }
            }
        }

【问题讨论】:

    标签: c# authentication token


    【解决方案1】:

    在您的代码中,您使用“Basic”初始化AuthenticationHeaderValue,这意味着基于用户名和密码的基本身份验证。如果你有一个令牌,你可以这样做:

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ACCESS_TOKEN);
    

    将 ACCESS_TOKEN 替换为您拥有的令牌。

    这是最可能的解决方案,但我只能在这里猜测,因为我不知道您尝试访问的 API。如果仍然不起作用,请尝试省略“Bearer”。

    Reference

    【讨论】:

    • 我尝试更改为 Bearer - 但没有成功 :-( 错误消息是“{StatusCode: 401, ReasonPhrase: '', Version: 1.1, Content: System.Net.Http. StreamContent, Headers: { Access-Control-Allow-Origin: * Access-Control-Allow-Methods: POST, GET, PUT, DELETE, HEAD, OPTIONS Access-Control-Allow-Headers: Origin, Allow, Accept, X-Requested -With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, RequestMD5 日期:2019 年 8 月 22 日星期四 11:00:16 GMT 服务器:Apache/2.4.29 服务器:(Ubuntu ) 内容长度:0 }}"
    • @MichaelEriksen 没有“Bearer”,只是令牌?请参阅我编辑的答案,我只能猜测。它也可以是“JWT”(如果你有 JWT 令牌)
    • 我也试过你编辑的答案,但没有成功:-(
    • 令牌只是一个普通令牌 - 而不是 JWT 令牌。
    • 问题是,我没有关于 API 的信息。我可以说“基本”是错误的,但除此之外它可能是任何东西。 HTTP 错误 401 也只是告诉您授权失败,但不是原因。它也可能是过期的令牌。你有更多关于 API 的信息吗?令牌是否应该是任何访问令牌,您是否在测试之前直接从该 API 获取它?
    猜你喜欢
    • 2022-01-12
    • 2015-09-12
    • 2012-09-12
    • 2021-01-04
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    相关资源
    最近更新 更多