The code is a bit generic (does not show how you get your signature for
example) but from experience I can tell you to check the following things:
1) Make sure that you are URL encoding everything that needs to be encoded
2) HttpUtility.UrlEncode sometimes produces lower-case characters in the
encoded string, this is refused by the server: either make your encode
function or make sure you have everything upper case.
3) You have to add your request parameters (all the parameters required by
the api method you are calling) in the big string that you use to generate
the signature (these parameters do NOT have to go in the authorization
header)
4) Parameter values ALSO have to be URL encoded
5) Make sure that the parameters used for generating the signature and the
parameters in the authorization header are sorted alphabetically
6) The parameters that have to be passed to the api method (ie: post body,
post title) need to be written in the body of the POST request
I think there are a few other things, but this is really the major stuff. I
am in the process of completing my app, which includes a reusable library
for the tumblr v2 api in C# that I wrote from scratch. It is not full
production quality code yet, but it has been working pretty well for me so
far. If my day job does not kill me in the next few days I hope to be able
to share it on codeplex in the near future.
On Saturday, August 4, 2012 11:19:21 AM UTC-4, manimaran....@gmail.com
wrote:
> Hi
> I am trying to post a new text message in tumblr wall from C# Web
> Application.. But I'm getting the "The remote server returned an error:
> (401) Unauthorized." error message when i try to post message. I'm using
> the following url (
> http://api.tumblr.com/v2/blog/upfrontApp.tumblr.com/post) to post a text
> message. The upfrontApp.tumblr.com is my tumblr application name.. I'm
> trying fix this error from last two, three weeks but i can not get a
> correct solution.. Please help me. Please give me any suggestion or idea..
> Your any suggestion or idea will be helping me lot.. I'm waiting for your
> response. Thanks a lot..
> this is my code to post a text message..
> url= http://api.tumblr.com/v2/blog/upfrontApp.tumblr.com/post
> method = POST or PUT
> postData = my test data
> public string APIWebRequest(string method, string url, string postData)
> {
> Uri uri = new Uri(url);
> string nonce = this.GenerateNonce();
> string timeStamp = this.GenerateTimeStamp();
> string outUrl, querystring;
> string sig = this.GenerateSignature(uri,
> this.ConsumerKey,
> this.ConsumerSecret,
> this.Token,
> this.TokenSecret,
> method,
> timeStamp,
> nonce,
> out outUrl,
> out querystring);
> HttpWebRequest webRequest = null;
> webRequest = System.Net.WebRequest.Create(url) as
> HttpWebRequest;
> webRequest.ContentType = "application/x-www-form-urlencoded";
> // text/xml
> webRequest.Method = method;
> webRequest.Credentials = CredentialCache.DefaultCredentials;
> webRequest.AllowWriteStreamBuffering = true;
> webRequest.PreAuthenticate = true;
> webRequest.ServicePoint.Expect100Continue = false;
> ServicePointManager.SecurityProtocol =
> SecurityProtocolType.Ssl3;
> webRequest.Headers.Add("Authorization", "OAuth realm=\"
> http://api.tumblr.com/\",oauth_consumer_key=\"" + this.ConsumerKey +
> "\",oauth_token=\"" + this.Token +
> "\",oauth_signature_method=\"HMAC-SHA1\",oauth_signature=\"" +
> HttpUtility.UrlEncode(sig) + "\",oauth_timestamp=\"" + timeStamp +
> "\",oauth_nonce=\"" + nonce + "\",oauth_verifier=\"" + this.Verifier +
> "\",oauth_version=\"1.0\"");
> if (postData != null)
> {
> byte[] fileToSend = Encoding.UTF8.GetBytes(postData);
> webRequest.ContentLength = fileToSend.Length;
> Stream reqStream = webRequest.GetRequestStream();
> reqStream.Write(fileToSend, 0, fileToSend.Length);
> reqStream.Close();
> }
> string returned = WebResponseGet(webRequest);
> return returned;
> }
> by
> Mani