C# - HttpClient将url重定向到登录,因此POST被发送到错误的URL

时间:2017-08-30 17:38:49

标签: c# linq-to-twitter

我目前正在使用 linqToTwitter 来通过Twitter对我的winforms应用程序进行身份验证,这需要我以后需要的信息来发送用户名更改请求。

在用户验证自己后,他应输入所需的用户名,应用程序将检查用户名是否可用。到目前为止,这很有效。

我的问题:

API不提供更新用户名的功能,因此我开始使用似乎成功发送请求的HttpClient来试试运气。但HttpClient似乎没有通过Twitter验证。甚至想到我' ve使用桌面应用程序通过oauth请求创建的authenticity_token

在使用fiddler记录请求时,我意识到更新配置文件URL正在将我重定向回登录站点。这意味着我的数据不会在我想要结束的地方结束。

Screenshot of the Fiddler response uploaded using Gyazo..

我一直在尝试更多这样的事情:首先将HttpClient发送到我在github的应用程序中找到的Twitter身份验证网站,这很遗憾无法正常工作。

有没有人有想法,我做错了什么?我感谢任何建议/帮助。

其他一些信息:

我知道twitter API允许更改某些update_profile个变量,但它不提供更改用户名。

应处理用户名更改的代码:

public async Task ChangeHandle(string Handle)
{
    using (HttpClientHandler handler = new HttpClientHandler() { CookieContainer = Cookies, UseCookies = true, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })
    using (HttpClient client = new HttpClient(handler))
    using (HttpRequestMessage request = new HttpRequestMessage() { Method = HttpMethod.Post, RequestUri = ChangeHandleUri, Content = new FormUrlEncodedContent(changehandle_request) })
    {
        try
        {
            string userAuthPassword = bunifuMetroTextbox2.Text;

            changehandle_request.Add(new KeyValuePair<string, string>("_method", "PUT"));
            changehandle_request.Add(new KeyValuePair<string, string>("authenticity_token", userInformations[1]));
            changehandle_request.Add(new KeyValuePair<string, string>("orig_uname", userInformations[2]));
            changehandle_request.Add(new KeyValuePair<string, string>("orig_email", userData[1]));
            changehandle_request.Add(new KeyValuePair<string, string>("user[screen_name]", Handle));
            changehandle_request.Add(new KeyValuePair<string, string>("user[email]", userData[1]));
            changehandle_request.Add(new KeyValuePair<string, string>("user[lang]", userData[2]));
            changehandle_request.Add(new KeyValuePair<string, string>("user[time_zone]", userData[3]));
            changehandle_request.Add(new KeyValuePair<string, string>("user[country]", userData[2]));
            changehandle_request.Add(new KeyValuePair<string, string>("user[requires_login_verification]", "1"));
            changehandle_request.Add(new KeyValuePair<string, string>("login_verification_enabled", "false"));
            changehandle_request.Add(new KeyValuePair<string, string>("user[no_username_only_password_reset]", "1"));
            changehandle_request.Add(new KeyValuePair<string, string>("user[autoplay_disabled]", "1"));
            changehandle_request.Add(new KeyValuePair<string, string>("user[personalize_timeline]", "1"));
            changehandle_request.Add(new KeyValuePair<string, string>("auth_password", userAuthPassword));

            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Host = "twitter.com";
            client.DefaultRequestHeaders.Referrer = new Uri("https://twitter.com/settings/account");
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
            client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");

            var response = await client.SendAsync(request);
            var responseString = await response.Content.ReadAsStringAsync();

            if (responseString.Contains("Thanks, your settings have been saved."))
            {
                MessageBox.Show("Handle Changed Successfully", "Response");
            }
            else
            {
                MessageBox.Show("Handle could not be changed", "Error");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码无法运行有两个原因:

  1. 您尚未在身份验证标头中包含正确的OAuth字符串。您可以访问Twitter API OAuth页面,了解有关如何实施此功能的更多信息。

  2. 您正在尝试更新Twitter API无法使用的参数。您可以访问Twitter API update_profile端点定义页面以查看可用的参数。

  3. 您提到您正在使用LINQ到Twitter。这可能更容易:

            var user = await twitterCtx.UpdateAccountProfileAsync(
                name: "Joe Mayo",
                url: "https://github.com/JoeMayo/LinqToTwitter",
                location: "Las Vegas, NV",
                description: "Testing the Account Profile Update with LINQ to Twitter.",
                includeEntities: true,
                skipStatus: true);
    

    您可以在LINQ to Twitter Console Samples page for AccountDemo.cs找到此代码的完整演示。