Monday.com & RestSharp API 请求

Monday.com & RestSharp API Request

我正在尝试将我的第一个请求写入 Monday.com,但无法弄清楚为什么我的请求返回“StatusCode:NotAcceptable”。我尝试了几种不同的方法来发送查询,但不确定是不是问题所在。有没有人以前做过这个或以前见过这个问题?我更换了我的 boardI,还尝试了 Get 和 Post。 感谢您的帮助!

    static async Task Main(string[] args)
    {
        //ReadXMLFileForSettings.ReadConfig();

        var client = new RestClient("https://api.monday.com/v2/");

        RestRequest request = new RestRequest() { Method = Method.Post };
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Authorization", $"{APIToken}");
        request.AddParameter(new JsonParameter("query", "query { boards(ids: 1234) { owner{ id } columns { title type } } }"));
        
        
        //request.AddParameter("query", "query { boards(ids: 1578294790) { owner{ id } columns { title type } } }");
        //request.AddJsonBody(new
        //{
        //    query = "query { boards(ids: 1578294790) { owner{ id } columns { title type } } }"

        //});

        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());
            }
        }
    }

试试这个:

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.monday.com/v2/");

request.Headers.Authorization = new AuthenticationHeaderValue(token);

string json =
    System.Text.Json.JsonSerializer.Serialize(
        new
        {
            query = "{ boards(ids: 1234) { owner{ id } columns { title type } } }"
        }
    );
request.Content = new StringContent(json,
    Encoding.UTF8, "application/json");

var response = await client.SendAsync(request);
var responseText = await response.Content.ReadAsStringAsync();
// TODO handle response

更新:RestSharp

var client = new RestClient("https://api.monday.com/v2/");

RestRequest request = new RestRequest() { Method = Method.Post };
request.AddHeader("Authorization", token);

//Here is the main problem:
//Idk why, but RestSharp adds the default accept header with value
//application/json, text/json, text/x-json, text/javascript, *+json, application/xml, text/xml, *+xml, *
// I didn't asked it for that!
request.AddHeader("Accept", "*/*");


string json =
    System.Text.Json.JsonSerializer.Serialize(
        new
        {
            query = "{ boards(ids: 1234) { owner{ id } columns { title type } } }"
        }
    );

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

var response = await client.ExecuteAsync(request);
// TODO handle response

这里的问题是星期一 API 不是 JSON API,而是 GraphQL。最好的使用方法是为 .NET 找一个 GraphQL 客户端,例如 https://github.com/graphql-dotnet/graphql-client

能够通过将其更改为 HttpClient 来使其正常工作。这是代码:

        HttpClient client = new HttpClient();

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.monday.com/v2/");

        request.Headers.Authorization = new AuthenticationHeaderValue(MondayApiKey);

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

        request.Content = new StringContent(json,Encoding.UTF8, "application/json");

        var response = await client.SendAsync(request);
        var responseText = await response.Content.ReadAsStringAsync();