c# - 在 Tumblr 上创建博客帖子时 OAuth 失败(不断收到 401/未授权)

标签 c# .net oauth tumblr

在尝试创建 new blog posting 时,我不断收到 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"
Content-Type: application/x-www-form-urlencoded
Host: api.tumblr.com
Content-Length: 130
Expect: 100-continue
Connection: Keep-Alive

来自 Tumblr 的回复:

HTTP/1.1 100 Continue

从客户端到 Tumblr:

type=quote&quote=Taking+Input+in+C%2b%2b+without+Library+Functions&source=http%3a%2f%2fstackoverflow.com%2fquestions%2f11959696%2f

来自 Tumblr 的回复:

HTTP/1.1 401 Not Authorized
Date: Tue, 14 Aug 2012 19:50:56 GMT
Server: Apache
P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Vary: Accept-Encoding
X-Tumblr-Usec: D=293868
Content-Length: 60
Connection: close
Content-Type: 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/未授权?

最佳答案

有同样的问题(但对于 iOS):我使用的 OAuth 库似乎没有使用 POST 数据字段作为签名。在我将字段 typecaption 添加为查询字符串后,OAuth 库使用了它们,我可以上传照片/帖子/其他内容。

似乎每个帖子正文字段(二进制数据除外)都需要成为 OAuth 签名的一部分。

关于c# - 在 Tumblr 上创建博客帖子时 OAuth 失败(不断收到 401/未授权),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959863/

相关文章:

c# - 防止 ControlParameter AutoPostBack 触发 ObjectDataSource Select 方法

.net - 如何从 Windows Server Core 上的 WPF 应用程序使用“浏览文件夹”对话框?

javascript - OAuth 2.0 和 Phonegap 的 InAppBrowser 进行身份验证

python - 你如何在 Django 中使用 rauth?

oauth - 系统.IO.FileNotFoundException : 'Microsoft.Framework.ApplicationHost'

c# - GUID 的 COM SAFEARRAY 从 C++ 返回到 C#

c# - 为什么我从 CultureInfo 得到的时间格式不正确?

c# - Entity Framework 返回 0 项

c# - 将日期时间 js 转换为 c#

c# - 未找到具有不变名称 'System.Data.SqlClient' 的 ADO.NET 提供程序的 Entity Framework 提供程序。