0

I am trying too make a rest call with C# for the first time. I think im very close but i get an error message : Error: 400 - Bad Request. Here is my Code:

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.test123.com/oauth2/token"))
    {

        string webstring = String.Format("grant_type=authorization_code&code={0}&redirect_uri={1}&client_id=${2}&client_secret={3}", access_code_string, RedirectURI,ClientId, ClientSecret);
        Console.WriteLine(webstring);
        request.Content = new StringContent(webstring);
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

        var response = await httpClient.SendAsync(request);
        Console.WriteLine("Access token: " + response);
    }
}

Here is an sample code from Curl

curl -X POST \
    --header "Content-Type: application/x-www-form-urlencoded" \
    --data "grant_type=authorization_code&code=dlZE0KFxhM&redirect_uri=http%3A%2F%2Fclient%2eexample%2Ecom&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET"
    "https://api.test123.com/oauth2/token"

This is the description:

Obtain an access token
After you have received the authorization code you can request an access token.

Method
POST

URL
https://api.test123.com/oauth2/token

Request format
application/x-www-form-urlencoded

Request parameters
Name    Type    Description
grant_type  String  Value must be set to authorization_code
code    String  The authorization code
client_id   String  
client_secret   String  
redirect_uri    String  The URL where the response is sent. Must match the registered redirect URI.
Response format
application/json
Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
Kickdak
  • 197
  • 1
  • 3
  • 15
  • You probably need to URL Encode `webstring`. – Crowcoder Feb 09 '20 at 12:23
  • Got error : 500 - Server Error: I tried adding this one: `string new_string = WebUtility.UrlEncode(webstring);` – Kickdak Feb 09 '20 at 12:42
  • I assume that's after you Format it? Trace the call with Fiddler and compare what is being sent with what you are using in curl. A 500 error could indicate a bug in the server. – Crowcoder Feb 09 '20 at 12:52
  • I think you should build uri string with parameters and create a request with this full url. Not to pass in Content. Here you can read about useful api for buikding urls https://stackoverflow.com/questions/17096201/build-query-string-for-system-net-httpclient-get – Anton Feb 09 '20 at 12:57
  • @Anton it's a POST, not a GET and the format is defined as application/x-www-form-urlencoded – Crowcoder Feb 09 '20 at 13:04
  • @Crowcoder oh yes. Sorry. You are definitely right – Anton Feb 09 '20 at 13:49

1 Answers1

0

I took an different approach

public void call()
    {

        string access_code_string = Read_Json_Values("accsess_code");
        string webstring = String.Format("https://api.test123.com/oauth2/token?grant_type=authorization_code&code={0}&client_id=${1}&client_secret={2}&redirect_uri={3}", access_code_string, ClientId, ClientSecret, RedirectURI);
        var client = new RestClient(webstring);
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddHeader("Authorization", "Bearer xxxx");
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
        Console.WriteLine(response.Content);

    }
Kickdak
  • 197
  • 1
  • 3
  • 15