为什么我的POST方法返回“未经授权"? C# [英] Why is my POST method returning "Unauthorized"? C#

查看:477
本文介绍了为什么我的POST方法返回“未经授权"? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://static.pmmlabs.ru/OpenPeriscope/

我遵循身份验证"下的文档,其中提供了示例和错误响应.当我调用POST方法时,我当前收到此错误消息:

I follow the documentation under "Authentication" where they provide examples and error responses. I am currently receiving this error message when I call my POST-method:

401 Unauthorized This endpoint requires authentication and no cookie value was provided, or is invalid

令牌应该绝对包含正确的值,因此我认为它必须与代码一起使用.这是我当前的代码:

The token should absolutely contain the correct value so I assume it has to be something with the code. This is my current code:

var client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
string json = "{" +
            "\"bundle_id\":\"\"," +
            "\"phone_number\":\"\"," +
            "\"session_key\":\"" + key + "\"," +
            "\"session_secret\":\"" + secret + "\"," +
            "\"user_id\":\"" + userid + "\"," +
            "\"user_name\":\"" + name + "\"," +
                        "\"vendor_id\":\"81EA8A9B-2950-40CD-9365-40535404DDE4\"" +
                        "}";
var content = new StringContent(json);
System.Diagnostics.Debug.WriteLine(json);
var response = await client.PostAsync("https://api.periscope.tv/api/v2/loginTwitter", content);
var responseJson = await response.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(responseJson);

我放在一起的JSON也有效.如果我将其写在日志中,则它看起来像这样:

The JSON I put together is also valid. If I write it out in the log it looks like this:

{"bundle_id":"","phone_number":"","session_key":"752365274-loPQJPpyAtwuqcJJtTqk7KlRgbdGgVQ5ahBWuItr","session_secret":"2hY9kLYCwKC3rIGGMlCt523uOy08zc8SEc7w6WSlLLViD","user_id":"752365274","user_name":"My username","vendor_id":"81EA8A9B-2950-40CD-9365-40535404DDE4"}

我遇到了一个帖子,其中用户成功使它起作用,但是由于我使用Xamarin Forms(PCL)进行编码,因此无法使用"HttpWebRequest"方法. 使用Twitter登录API

I came across a post where a user successfully made it work but it was with "HttpWebRequest" method that I cannot use because I am coding in Xamarin Forms (PCL). Using Twitter login API

如果您查看他对自己帖子的回答,您将看到示例代码.

If you check his answer to his own post you can see the sample code.

如何调整当前代码以成功获取JSON响应?

How can I adjust my current code in order to successfully get the JSON response?

更新:

参数/令牌应该正确.一直尝试使用不同的解决方案/代码结构,但始终给我未经授权".这是我尝试使用的最后一个代码(试图模仿我链接的有效项目):

Parameters/tokens should be correct. Been trying with different solutions/codestructures but it keeps giving me "Unauthorized". This is the last code I tried to use (tried to mimic the project that I linked that works):

        var client = new HttpClient();
        var parameters = new Dictionary<string, string> {
            { "session_secret", secret},
            { "user_id", userid},
            { "phone_number", ""},
            { "vendor_id", ""},
            { "bundle_id", "com.bountylabs.periscope"},
            { "session_key", key},
            { "user_name", username}
        };

        var jsonRequest = JsonConvert.SerializeObject(parameters);
        MemoryStream jsonStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonRequest));
        HttpContent content = new StreamContent(jsonStream);

        var response = await client.PostAsync("https://api.periscope.tv/api/v2/loginTwitter", content);
        var responseJson = await response.Content.ReadAsStreamAsync();
        var streamReader = new StreamReader(responseJson);
        var str = streamReader.ReadToEnd();
        System.Diagnostics.Debug.WriteLine(str);

推荐答案

似乎授权会话密钥或机密存在问题. 如果我在您的位置,我将使用其他客户端来测试我的请求,以确保我传递正确的参数(尤其是"session_key"和"session_secret").

it seems that there is some problem with the authorization session key or secret. If I were in your place, I would test my request using some other client in order to make sure I am passing correct parameters (especially for "session_key" and "session_secret").

为了快速,简单地进行此测试,我建议使用Chrome扩展客户端"Restlet Client-DHC":

In order to do this test quickly and simply, I would recommend using the "Restlet Client - DHC" which is a Chrome extension client:

您可以使用以下链接添加此扩展名:

You can add this extension using the below link:

https://chrome.google.com/webstore/detail/restlet-client-dhc/aejoelaoggembcahagimdiliamlcdmfm?hl = zh-CN

这篇关于为什么我的POST方法返回“未经授权"? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆