ErrorThe remote name could not be resolved: 'api.tumblr.com

34 views
Skip to first unread message

Rajesh Tiwari

unread,
Nov 6, 2017, 1:50:34 AM11/6/17
to Tumblr API Discussion
Hello Everyone.

We are trying to POST blog through Tumblr API. We are getting the error as "The remote name could not be resolved: 'api.tumblr.com". 

Our Code:

                var oauth_token = fmSocialNetworksModel.msAccessToken;
                var oauth_token_secret = fmSocialNetworksModel.msAccessTokenSecret;
                var oauth_consumer_key = Properties.Settings.Default.TumblrAPIKey;
                var oauth_consumer_secret = Properties.Settings.Default.TumblrAPISecret;

                var oauth_version = "1.0";
                var oauth_signature_method = "HMAC-SHA1";
                var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(
                DateTime.Now.Ticks.ToString()));
                var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
                var status = "Updating status via REST API if this works";


                var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";

                var baseString = string.Format(baseFormat,
                oauth_consumer_key,
                oauth_nonce,
                oauth_signature_method,
                oauth_timestamp,
                oauth_token,
                oauth_version,
                Uri.EscapeDataString(status)
                );

                baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url),
                "&", Uri.EscapeDataString(baseString));



                var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                "&", Uri.EscapeDataString(oauth_token_secret));

                string oauth_signature;
                using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
                {
                    oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
                }

                var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                "oauth_version=\"{6}\"";



                var authHeader = string.Format(headerFormat,
                Uri.EscapeDataString(oauth_nonce),
                Uri.EscapeDataString(oauth_signature_method),
                Uri.EscapeDataString(oauth_timestamp),
                Uri.EscapeDataString(oauth_consumer_key),
                Uri.EscapeDataString(oauth_token),
                Uri.EscapeDataString(oauth_signature),
                Uri.EscapeDataString(oauth_version)
                );

                var postBody = "status=" + Uri.EscapeDataString(status);

                ServicePointManager.Expect100Continue = false;                

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
                request.Headers.Add("Authorization", authHeader);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";


                request.KeepAlive = false;                
                request.Proxy = null;

                using (Stream stream = request.GetRequestStream())
                {
                    byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                    stream.Write(content, 0, content.Length);
                }

                var Response = (WebResponse)request.GetResponse();
                StreamReader ResponseDataStream = new StreamReader(Response.GetResponseStream());
                string str = ResponseDataStream.ReadToEnd();//REMOVE
                dynamic stuffUserData = JsonConvert.DeserializeObject(str);

Can anyone let us know why are we getting the error as The remote name could not be resolved: 'api.tumblr.com/

Thanks and regards.
Raj

Michael Ceyko

unread,
Jan 3, 2018, 1:36:56 PM1/3/18
to Tumblr API Discussion
Hey Raj,

I've taken a look at your code sample, and I think the issue is that you're escaping the entirety of the URI.
Uri.EscapeDataString(resource_url);
 
This will result in a string of "http%3A%2F%2Fapi.tumblr.com%2Fv2%2Fblog%2Fbaskyt.tumblr.com%2Fpost%3Ftype%3Dtext%26body%3DHi", which is indeed not a valid URI.

The function Uri.EscapeDataString() should only be used on data that needs to be escaped for safety. It cannot be used directly on a full URI.

Hope this helps.
Regards,
Mike
Reply all
Reply to author
Forward
0 new messages