0

I want to implement authorization to Tumblr. During the reception access_token like this:

string request_url =
                 "https://www.tumblr.com/oauth/access_token" + "?" +
                 "oauth_consumer_key=" + oauth_consumer_key + "&" +
                 "oauth_token=" + main_oauth_token + "&" +
                 "oauth_signature_method=" + "HMAC-SHA1" + "&" +
                 "oauth_signature=" + Uri.EscapeDataString(oauth_signature) + "&" +
                 "oauth_timestamp=" + oauth_timestamp + "&" +
                 "oauth_nonce=" + oauth_nonce + "&" +
                 "oauth_version=" + "1.0" + "&" +
                 "oauth_verifier=" + oauth_verifier;

                HttpClient httpClient = new HttpClient();
                string responseBodyAsText = await httpClient.GetStringAsync(request_url);

I get the following error:

An exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Response status code does not indicate success: 401 (Unauthorized).

Callback URL i am set. What could be the problem?

goodniko
  • 163
  • 3
  • 14

2 Answers2

1

The problem is you're sending a GET request. Per the spec you must use POST with header ContentType: application/x-www-form-urlencoded. See here for a good example of doing this with HttpClient. Note that your name/value pairs are NOT part of the URL, even though they are encoded like a query string.

Alternatively, if you don't mind a library dependency, you could try my Flurl library, which shortens the code to this:

string responseBodyAsText = await "https://www.tumblr.com/oauth/access_token"
    .PostUrlEncodedAsync(new { 
        oauth_consumer_key = oauth_consumer_key,
        oauth_token = main_oauth_token,
        oauth_signature_method = "HMAC-SHA1",
        oauth_signature = oauth_signature,
        oauth_timestamp = oauth_timestamp,
        oauth_nonce = oauth_nonce,
        oauth_version = "1.0",
        oauth_verifier = oauth_verifier
    })
    .ReceiveString();

Flurl is available on NuGet.

Community
  • 1
  • 1
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
0

EnsureSuccessStatusCode() can also generate the same error. Check if you are using it, if yes then make sure your server authentication are correct properly.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Ashfaque
  • 11
  • 3