在尝试创建新博客帖子时,我不断收到来自 Tumblr 的 401/未授权。
这是我从网卡捕获的 HTTP 流量序列:
从客户到 Tumblr:
POST /v2/blog/topictastic.tumblr.com/post HTTP/1.1
Authorization: OAuth oauth_consumer_key="s83rTQBomoPtl66AT0hEScmYM0QYdhlbW0sa5sRIojgeGOcAUr", oauth_nonce="9313743", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1344973847", oauth_token="kw5NdFnuJ8CLC4cMOMJ4YsKXtOApdw6IcSoPJlF5GZgHaWo9zy" , oauth_version="1.0", oauth_signature="FLStmGNNdfhQysKDtcKSWFBY%2F%2F4%3D"
内容类型:application/x-www-form-urlencoded
主机:api.tumblr.com
内容长度:130
预期:100-继续
连接:活着
来自 Tumblr 的回复:
HTTP/1.1 100 继续
从客户到 Tumblr:
type=quote"e=Taking+Input+in+C%2b%2b+without+Library+Functions&source=http%3a%2f%2fstackoverflow.com%2fquestions%2f11959696%2f
来自 Tumblr 的回复:
HTTP/1.1 401 未授权
日期:2012 年 8 月 14 日星期二 19:50:56 GMT
服务器:Apache
P3P:CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
变化:接受编码
X-Tumblr-Usec:D =293868
内容长度:60
连接:关闭
内容类型:应用程序/json
{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}
这是我用来生成和发布请求的 C# 源代码:
var baseHostName = request.Endpoint.Properties["base-hostname"];
var postUrl = string.Format("http://api.tumblr.com/v2/blog/{0}/post", baseHostName);
using (var wc = new WebClient())
{
var oauth = new OAuthBase();
var consumerKey = request.ApplicationProperties["ConsumerKey"];
var consumerSecret = request.ApplicationProperties["ConsumerSecret"];
var oauthToken = request.Enrollment.Properties["oauth_token"];
var oauthTokenSecret = request.Enrollment.Properties["oauth_token_secret"];
var timestamp = oauth.GenerateTimeStamp();
var nonce = oauth.GenerateNonce();
string url, url2;
var values = new NameValueCollection();
var type = request.Notification.Properties["type"];
foreach (var property in request.Notification.Properties)
{
if (property.Key.StartsWith(type + "."))
{
var tumblrPropertyName = property.Key.Replace(type + ".", string.Empty);
values.Add(tumblrPropertyName, property.Value);
}
else if (!property.Key.Contains("."))
{
values.Add(property.Key, property.Value);
}
}
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();
throw new Exception(message, webException);
}
}
该类OAuthBase
位于此处。
在运行此代码时,我确实有一个有效的 oauth_token 和 oauth_token_secret。任何想法这有什么问题?为什么我不断收到 401/未授权?