Link to home
Start Free TrialLog in
Avatar of Dinesh Bali
Dinesh Bali

asked on

Unauthorized and BadRequest

Hi,

I am working on .net using c#

I am posting data in method PostAsJsonAsync using package System.Net.Http.

Please advise me with my findings.

I am getting  response as

ReasonPhrase = "Unauthorized" string
StatusCode = "BadRequest" System.Net.HttpStatusCode

Not sure but looks to me that url(https://edg-dev-sit.dfdegg.net/loyalty/identity/v2/ssotoken) where I am submitting the data is not allowing data to be submitted from myside.

or Allow origin is not done.

I am not sure. Please help me with my finding.

My code is
HttpResponseMessage response = mHttpClient.PostAsJsonAsync<T>(uri.ToString(), data).Result;

where 
uri.ToString() is https://edg-dev-sit.dfdegg.net/loyalty/identity/v2/ssotoken

and data is { grantType = "authorization_code", authorizationCode = "umKTS4bKXOedNSjuv2rq" }

Open in new window



mHttpClient is as follows:

System.Net.Http
private HttpClient mHttpClient = null;

mHttpClient = new HttpClient(httpClientHandler)
            {
                Timeout = timeout,
                DefaultRequestHeaders =
                {
                    AcceptEncoding =
                    {
                        StringWithQualityHeaderValue.Parse("gzip"),
                        StringWithQualityHeaderValue.Parse("deflate")
                    },
                    CacheControl = new CacheControlHeaderValue() { NoCache = true }
                }
            };

Open in new window


My response (HttpResponseMessage response) is as follows.
See screenshot attached.

Headers	{Connection: keep-alive
Access-Control-Allow-Origin: 
Access-Control-Allow-Headers: Authorization,requestId,correlationId,clientApiKey,Origin, accept, Content-Type,channelName
Date: Fri, 19 Jul 2019 12:15:15 GMT
}	System.Net.Http.Headers.HttpResponseHeaders


ReasonPhrase = "Unauthorized" string
StatusCode = "BadRequest" System.Net.HttpStatusCode
RequestMessage	{Method: POST, RequestUri: 'https://edg-dev-sit.dfdegg.net/loyalty/identity/v2/ssotoken', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[<>f__AnonymousType0`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], ESSSSO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Headers:
{
  Accept-Encoding: gzip
  Accept-Encoding: deflate
  Cache-Control: no-cache
  Authorization: Bearer J3S2ureewRsDA2PAq8hmI41Ya1K8
  requestId: 1907201912071388
  correlationId: 09001b33-79a1-4dc7-a184-6b8f9f9b17c9
  channelName: WEB_USER
  Content-Type: application/json; charset=utf-8
  Content-Length: 77
}}	System.Net.Http.HttpRequestMessage

Open in new window


User generated image
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

First, that's a terrible API that you're having to deal with. My condolences.

Does the API provide any documentation? It seems that you're most likely doing something wrong in the way you're passing in the data. The payload looks like it should be an OAuth request, but an OAuth request isn't JSON, it's a simple string, usually like:

grantType=authorization_code&authorizationCode=umKTS4bKXOedNSjuv2rq

You might want to try creating a StringContent object with the above string and using PostAsync (unless of course the API documentation explicitly calls for a JSON object such as the one you've constructed).
Avatar of Dinesh Bali
Dinesh Bali

ASKER

Thanks How you identified that this should be OAuth request?
Regarding PostAsync this is the way it works on other applications i.e., using method PostAsJsonAsync
ASKER CERTIFIED SOLUTION
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial