私は、v2 APIを使ってTumblrと対話するための簡単なアプリケーションを開発しようとしています。私はoAuthの流れのすべてのステップ(Twitterの文書のおかげで)を通過して、認可され、認証されたトークンと秘密を得ることができます。しかし、それは投稿になると、私は401の不正なエラーが発生します。これは関連するコードスニペットです:oAuthとC#を使ってTumblrに投稿する
private void post_Click(object sender, System.Windows.RoutedEventArgs e)
{
url = new Uri("http://api.tumblr.com/v2/blog/*myblog*.tumblr.com/post");
Random random = new Random();
nonce = random.Next(1234000, 9999999).ToString();
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
string method = "POST";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}&", method);
sb.AppendFormat("{0}&", upperCaseUrl(HttpUtility.UrlEncode(url.ToString())));
sb.AppendFormat("body%3D{0}%26", upperCaseUrl(HttpUtility.UrlEncode(textBox.Text)));
sb.AppendFormat("oauth_consumer_key%3D{0}%26", consumerKey);
sb.AppendFormat("oauth_nonce%3D{0}%26", nonce);
sb.AppendFormat("oauth_signature_method%3D{0}%26", "HMAC-SHA1");
sb.AppendFormat("oauth_timestamp%3D{0}%26", timestamp);
sb.AppendFormat("oauth_token%3D{0}%26", token);
sb.AppendFormat("oauth_version%3D{0}%26", "1.0");
sb.AppendFormat("type%3D{0}", "text");
System.Diagnostics.Debug.WriteLine(sb.ToString());
string signingKey = consumerSecret + '&' + secret;
HMACSHA1 signing = new HMACSHA1(Encoding.ASCII.GetBytes(signingKey));
byte[] bytearray = Encoding.ASCII.GetBytes(sb.ToString());
MemoryStream stream = new MemoryStream(bytearray);
signature = Convert.ToBase64String(signing.ComputeHash(stream));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
StringBuilder header = new StringBuilder();
header.Append("OAuth ");
header.AppendFormat("oauth_consumer_key=\"{0}\", ", consumerKey);
header.AppendFormat("oauth_token=\"{0}\", ", token);
header.AppendFormat("oauth_nonce=\"{0}\", ", nonce);
header.AppendFormat("oauth_timestamp=\"{0}\", ", timestamp);
header.AppendFormat("oauth_signature_method=\"{0}\", ", "HMAC-SHA1");
header.AppendFormat("oauth_version=\"{0}\", ", "1.0");
header.AppendFormat("oauth_signature=\"{0}\"", upperCaseUrl(HttpUtility.UrlEncode(signature)));
System.Diagnostics.Debug.WriteLine(header.ToString());
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Post;
request.Headers.Add("Authorization", header.ToString());
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
if (reader.ReadToEnd() == "201: Created")
{
control.Invoke((MethodInvoker)delegate
{
statusText.Text = "Post successfully sent!";
});
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
この関数は、ボタンをクリックしてテキストをテキストボックスから取得するときに呼び出されます。署名部分は、他のサービス(Twitterなど)でテストされており、Tumblr自体に対して有効な署名を提供しているため、正しいです。 Authorizationヘッダーは、トークンを要求してそれを承認するために使用したものとほぼ同じです。基本文字列には、oauth_ *標準パラメータ以外に、コンテンツのタイプ(この場合はテキスト)と本文のみが含まれています。私はhueniverse.comで利用可能なツールでコードをチェックしましたが、まだ何もありません。
おそらく、一部のHTTPヘッダーが欠落しているか、誤った要求を送信しています。何か案は?ありがとう。あなたを助けることがgithubの上の完全な例がある
あなたはそれを修正する方法上の任意の更新?私も同様の問題があります。 401をTumblrから取得し続けますが、理由は不明です。 –
同じ問題がある...あなたはそれを(そしてどのように)修正することができましたか? – SimpleVar