首页 > 解决方案 > 是否有可能使用 OAuth 1.0 发送带有正文的帖子?

问题描述

我正在尝试向 WordPress API 发送带有正文的 POST 请求。我仍然收到 401 错误。

我决定使用:https ://gist.github.com/DeskSupport/2951522通过 OAuth 1.0 进行授权,它与 GET 方法完美配合。然后我想实现另一种发送简单正文的方法。

那是我的代码:

            var oauth = new OAuth.Manager();
            oauth["consumer_key"] = _consumerKey;
            oauth["consumer_secret"] = _consumerSecret;
            oauth["token"] = _accessToken;
            oauth["token_secret"] = _tokenSecret;


            var appUrl = _baseUrl + url;
            var authzHeader = oauth.GenerateAuthzHeader(appUrl, "POST");

            string body = GenerateBody(parameters);
            byte[] encodedData = Encoding.ASCII.GetBytes(body);

            var request = (HttpWebRequest)WebRequest.Create(appUrl);
            request.Method = "POST";
            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;
            request.Headers.Add("Authorization", authzHeader);
            request.ContentLength = encodedData.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            Stream newStream = request.GetRequestStream();
            newStream.Write(encodedData, 0, encodedData.Length);

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {

                }
            }

方法 GenerateBody 的结果是user_login=login&user_pass=BXE&04K44DoR1*a

我还尝试将 '&' 字符更改为 '%26' 但它不起作用。

这个请求通过邮递员工作,我不知道出了什么问题。

标签: c#.networdpresspostoauth

解决方案


好的,我找到了解决方案。

https://blog.dantup.com/2016/07/simplest-csharp-code-to-post-a-tweet-using-oauth/

这家伙写了提出这个请求的方法。同样重要的是,您必须将 oauth_nonce 更改为唯一令牌。


推荐阅读