简体   繁体   English

HttpClient 不返回 URI 的 json 值

[英]HttpClient not returning json value of URI

I am trying to use HttpClient to GET information from Jira, but I am unable to see any of the information.我正在尝试使用 HttpClient 从 Jira 获取信息,但我看不到任何信息。 I want to be able to get all the bugs that match certain filters so that I can add them to a table in my program.我希望能够获得与某些过滤器匹配的所有错误,以便我可以将它们添加到我的程序中的表中。

I have tried to access Jira with the rest api, but every time I do it says that the issue or project doesn't exist.我曾尝试使用 rest api 访问 Jira,但每次我这样做时都说问题或项目不存在。 The thing is that if I enter the URI into the bar at the top of my browser I can see the JSON text that I want.问题是,如果我在浏览器顶部的栏中输入 URI,我可以看到我想要的 JSON 文本。 This leads me to believe that the reason my code is not returning these values is because of an authorization issue.这让我相信我的代码没有返回这些值的原因是因为授权问题。 I am using basic auth to send my credentials.我正在使用基本身份验证来发送我的凭据。 I also want to add that I used cURL in cmd to test my credentials with basic auth and it worked.我还想补充一点,我在 cmd 中使用了 cURL 来使用基本身份验证测试我的凭据,并且它有效。

public async Task<JiraModel> GetBugs()
        {
           using (var client = new HttpClient())
            {
                string url = "https://myurl.atlassian.net/rest/api/3/project/VCMF";

                String username = "username";
                String password = "apikey";
                String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(username + ":" + password));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "Basic " + encoded);
                client.BaseAddress = new Uri("https://myurl.atlassian.net/rest/api/3/project/VCMF");
                var response = await client.GetAsync(url);
                var content = await response.Content.ReadAsStringAsync();
                return JsonConvert.DeserializeObject<JiraModel>(content);
            }
        }

I should be getting the json results in string form by the end of this length of code, but I keep getting a 404 error instead that for this code specifically says "No project could be found with key 'VCMF'".在这段代码的末尾,我应该得到 json 结果为字符串形式,但我一直收到 404 错误,而不是因为这段代码明确表示“找不到带有密钥 'VCMF' 的项目”。

The issue here is that you're creating the authorization header incorrectly.这里的问题是您错误地创建了授权 header。

The constructor you're using for AuthenticationHeaderValue class takes two arguments: scheme and parameter:您用于AuthenticationHeaderValue class 的构造函数需要两个 arguments:方案和参数:

public AuthenticationHeaderValue(string scheme, string parameter)
{
}

The first argument should be the scheme (Basic in this case) and the second, the base64-encoded credentials:第一个参数应该是方案(在本例中为基本),第二个参数应该是 base64 编码的凭据:

So instead of:所以而不是:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "Basic " + encoded);

It should be:它应该是:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM