c# - 是否有可能使用 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' 但它不起作用。
这个请求通过邮递员工作,我不知道出了什么问题。
解决方案
好的,我找到了解决方案。
https://blog.dantup.com/2016/07/simplest-csharp-code-to-post-a-tweet-using-oauth/
这家伙写了提出这个请求的方法。同样重要的是,您必须将 oauth_nonce 更改为唯一令牌。
推荐阅读
- javascript - React Native,所有项目的状态更改而不是单击的项目
- json - 调用从 react-app 返回 JSON 的 REST API
- c++ - 有什么方法可以在 cmake 中检测程序的版本吗?
- python - 如何通过 Python 脚本在 Windows 资源管理器中打开文件夹?
- constructor - Three.js THREE.DeviceOrientationControls 不是构造函数
- java - 没有最后一个节点的 LinkedList 的实现
- django - 将数据从两个模型传递到一个 html 页面 - Django
- javascript - 如何根据登录的用户角色禁用 HTML 文档的某些元素?
- java - 如何从 S3 加载 spring application.yml
- windows - 如何从提升的 AHK 脚本启动未提升的 exe?