我正在尝试在Tumblr上创建一篇文章博客文章。 当我运行此代码时,我收到错误:“未授权”
我相信我在POST方法中传递了签名。我不擅长这样做,所以我无法理解我做错了什么。
我使用过这个.cs类:“ OAuthBase ” https://gist.github.com/tsupo/112124#file-oauthbase-cs
代码:
var postUrl = "http://api.tumblr.com/v2/blog/this-is-my-blog-name.tumblr.com/post";
using (var wc = new WebClient())
{
var oauth = new OAuthBase();
var consumerKey = "1j1j1j1j1j1j1j1j1";
var consumerSecret = "2j2j2j2j2j2j2j2j";
var oauthToken = "3j3j3j3j3j3j3j3j3j3";
var oauthTokenSecret = "4j4j4j4j4j4j4j4j44j";
var timestamp = oauth.GenerateTimeStamp();
var nonce = oauth.GenerateNonce();
string url, url2;
var values = new System.Collections.Specialized.NameValueCollection();
//This is the blog post title and text
values.Add("title", "Title of blog post");
values.Add("type", "text");
values.Add("body", "This is the Blog text");
var postUri = new Uri(postUrl);
var signature = oauth.GenerateSignature(
postUri,
consumerKey,
consumerSecret,
oauthToken,
oauthTokenSecret,
"POST",
timestamp,
nonce,
null,
out url,
out url2);
var urlEncodedSignature = oauth.UrlEncode(signature);
var authHeader = "OAuth " + url2 + "&oauth_signature=" + urlEncodedSignature + "\"";
authHeader = authHeader.Replace("&", "\", ").Replace("=", "=\"");
wc.Headers.Add("Authorization", authHeader);
try
{
wc.UploadValues(postUri, values);
}
catch (WebException webException)
{
var message = (new StreamReader(webException.Response.GetResponseStream())).ReadToEnd();
MessageBox.Show(message.ToString());
}
}