Muds
Muds

Reputation: 4116

Rest API Responds with 403 Forbidden from application, works fine if called from Postman

I am trying to Post to a Rest API that uses OAuth 1.0 for authentication. API providers have provided me with Token and Secrets and Consumer key using which I can successfully create an OAuth Header that works with PostMan

But problem is when I try to use same Headers and Body using HttpClient it throws 403 Forbidden at me.

I have made sure that there are no discrepancies in Headers or Body or URL

Seems there is something Extra/Less that PostMan does that am not able to recreate in C#

private static async Task<string> SendRequest(string fullUrl, string oAuthHeader)
        {
            var json = GetJsonPayload();
            var sc = new StringContent(json, Encoding.UTF8, "application/json");
            sc.Headers.Add("ContentType", "application/json");

            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Add("Authorization", oAuthHeader);

                var httpResp = await http.PostAsync(fullUrl, sc);
                httpResp.EnsureSuccessStatusCode();

                var respBody = await httpResp.Content.ReadAsStringAsync();

                return respBody;
            }
        }

Here my OAuth string looks like this --

OAuth realm="REALM", oauth_consumer_key="CONSUMERKEY", oauth_nonce="NFcdqA", oauth_signature_method="HMAC-SHA1", oauth_signature="GENERATEDSIGNATURE", oauth_timestamp="1555457636", oauth_token="TOKEN", oauth_version="1.0"

I have observed that in Postman, if in Authorization Tab I select Type as No Auth and set a manual header to Authorization with given OAuth string Postman also throws 403, but selecting Authorization Type to OAuth 1.0 works, I cant seem to find why is that and how to set Type in C#

Any help/pointers will be highly appreciated.

Upvotes: 1

Views: 6496

Answers (1)

Nutatello
Nutatello

Reputation: 89

In Postman, under the "Send" button, click the "Code" button - this has code generators for every language - select C# and try that, if not generate a curl and verify you have every piece.

Upvotes: 1

Related Questions