在尝试创建new blog posting时,我一直从Tumblr获得401 /未授权 .
以下是从网卡捕获的HTTP流量序列:
从客户到Tumblr:
POST /v2/blog/topictastic.tumblr.com/post HTTP / 1.1授权: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-continue连接: 活着
来自Tumblr的回复:
HTTP / 1.1 100继续
从客户到Tumblr:
type = quote&quote =在没有库函数的情况下获取C%2b%2b中的输入&source = http%3a%2f%2fstackoverflow.com%2fquestions%2f11959696%2f
来自Tumblr的回复:
HTTP / 1.1 401未授权日期:星期二,2012年8月14日19:50:56 GMT服务器:Apache P3P:CP =“所有ADM DEV PSI COM我们的OTRo STP IND ONL”变化:接受编码X-Tumblr-Usec: D = 293868内容长度:60连接:关闭内容类型:application / 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
类是located here .
在我运行此代码时,我确实有一个有效的oauth_token和oauth_token_secret . 有什么想法这有什么不对吗?为什么我会继续获得401 /不被授权?
2 回答
有同样的问题(但使用iOS):似乎我使用的OAuth库没有使用POST数据字段进行签名 . 在我将字段 type 和 caption 另外添加为查询字符串之后,OAuth库使用了它们,我能够上传照片/帖子/其他内容 .
似乎每个帖子正文字段(二进制数据除外)都需要成为OAuth签名的一部分 .
您似乎需要更改编码 . 例如,空格应为“%20”而不是“”符号!