C# API POST with JSON

For anyone trying to work with Xibo and C#… tis may help

    private async void button4_Click(object sender, EventArgs e)
    {
        string clientId = "your_clientId";
        string clientSecret = "your_clientSecret";
        string uri = "http://srv1.youservername:8080/api/authorize/access_token";

        var formContent = new FormUrlEncodedContent(new[]
        {
              new KeyValuePair<string, string>("client_id", clientId),
              new KeyValuePair<string, string>("client_secret", clientSecret),
              new KeyValuePair<string, string>("grant_type", "client_credentials")
        });

        var myHttpClient = new HttpClient();
        myHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var response = await myHttpClient.PostAsync(uri.ToString(), formContent);//GivesValue ok Request 
        var stringContent = await response.Content.ReadAsStringAsync();//Gives Better Erro and Data
        statustextBox1.Text = stringContent;

    }
1 Like

In case anyone wonders what this is - I think it gives you an access_token you can add as an Authorization: Bearer <access_token> header to subsequent calls.

I’m not sure why you have this in front of your client_secret?

I first posted it with my correct key… So I had to quickly add some char. (smile)

1 Like

When I try your code, I get this as response:
The requested URL /api/authorize/access_token was not found on this server

The URI I use is : http://IP.IP.IP.IP/api/authorize/access_token