C# API Response StatusCode:Unauthorized

Hey everyone,
Trying to make my first Monday.com API and having issues with my response. It comes back as Unauthorized. I made sure to have Admin authorization. When I use the Monday.com developer section to try the query it works fine and returns my table. Is there something I’m missing? Thank you for any help!!

        var client = new RestClient("https://api.monday.com/v2");
        RestRequest request = new RestRequest() { Method = Method.Post };

        
        //request.AddHeader("Cookie", "__cfduid = d580475ff5eb080ca4afa8689679a21e61572616637");
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Authorization", "Bearer" + "XXXXXX");
        request.AddJsonBody(new
        {
            query = "{\"query\": \"{boards(limit:1){id name}}\"}"
        }); 
        var response = await client.ExecuteAsync(request);
        var responseWorkloads = JObject.Parse(response.Content).SelectToken("boards");
        var responseWorkloadsItems = responseWorkloads.SelectToken("items");

        foreach (JObject value in responseWorkloadsItems)
        {
            foreach (var property in value.Properties())
            {
                Logging.WriteToLog(property.Name);
                Logging.WriteToLog(property.Value.ToString());
            }
        }

Hello @Meneghini !

Where are you getting your access key from? (please do not share the key, only the source of it).

If you use the header in this way, you should be able to make it work:

// 	headers: {
// 		'Content-Type': 'application/json',
// 		'Authorization': YOUR_ACCESS_KEY
// 	},

Please let me know if this helps!

Cheers,
-Matias

Hello!

I am getting the access key by clicking on my profile - Admin - API - API v2 Token. Is this not the correct way?

Looking into it more, I might need to do The OAuth Flow?

@Meneghini that is correct.

What happens if you do not use “Bearer” and just use request.AddHeader(“Authorization”, “XXXXXXXXX”)?

I ask this because that is the way I see it is usually used.

I’ve updated my code and getting a new error now. It’s due to the Json query. It returns “no query string was present”

RestRequest request = new RestRequest() { Method = Method.Post };
request.AddHeader(“Authorization”, $"{APIToken}");
request.AddHeader(“Accept”, “/”);

        string json =
        System.Text.Json.JsonSerializer.Serialize(
            new
            {
                query = "{boards(limit:1) {id name}}"
            }
        );

        request.AddJsonBody(json, "application/json");

Hello @Meneghini!

Being transparent with you, I have no experience with C# so I can’t advise you on what you can change in your code to make this work.

Having said that, the API is expecting a query value to be passed. I made an example on Postman and exported it so you might have something to follow:

Here is how Postman exported it in “C# RestSharp”:

var client = new RestClient("https://api.monday.com/v2");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "MY_ACCESS_TOKEN");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Cookie", "XXXXXXXXX");
request.AddParameter("application/json", "{\"query\":\"query{\\nboards(limit:1) {id name}\\n}\",\"variables\":{}}",
           ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Is this useful?

Hope you can make this work!

Cheers,
-Matias

Thank you so much for the help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.