1

我正在尝试使用 .net HttpClient() 为 Calendly 创建一个 webhook 订阅

https://developer.calendly.com/docs/webhook-subscriptions

我正在尝试将此 Curl 命令转换为 .Net

curl --header "X-TOKEN: <your_token>" --data "url=https://blah.foo/bar&events[]=invitee.created&events[]=invitee.canceled" https://calendly.com/api/v1/hooks

这是我的 .Net 代码:

private static async Task<HttpResponseMessage> PostCreateWebhookSubscription()
{
    var client = new HttpClient {BaseAddress = new Uri("https://calendly.com")};
    var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/hooks/");
    var keyValues = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("url",
            "https://requestb.in/17ruxqh1&events[]=invitee.created&events[]=invitee.canceled")
    };
    request.Content = new FormUrlEncodedContent(keyValues);
    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") {CharSet = "UTF-8"};
    request.Content.Headers.Add("X-TOKEN", "<my_calendly_token>");
    return await client.SendAsync(request);
}

我收到此错误 422 错误,但无法弄清楚要更改哪些内容才能使其正常工作。

收到错误无法处理的实体

{"type":"validation_error","message":"验证失败","errors":{"events":["不能为空"]}}

我能够运行 Curl 命令并且它在同一台机器上运行良好,所以我知道它正在运行。

我创建了一个 .net HttpClient 调用来测试基本令牌,它运行良好。

有什么建议么?

4

1 回答 1

3

终于回来了,当我再次查看代码时,它就明白了。

最初我将整个 URL 视为一个大字符串,但一开始并没有意识到 & 符号是分隔要传递的值的符号。

下面的坏代码:

var keyValues = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("url",
        "https://requestb.in/17ruxqh1&events[]=invitee.created&events[]=invitee.canceled")
};

应该将其更改为将每个值与其文档分开:

var keyValues = new List<KeyValuePair<string, string>>
{
     new KeyValuePair<string, string>("url","https://requestb.in/17ruxqh1"),
     new KeyValuePair<string, string>("events[]","invitee.created"),
     new KeyValuePair<string, string>("events[]","invitee.canceled")
};

因此,对于那些想要使用 .Net 通过 Calendly 创建 webhook 订阅的人来说,这里有一个完整的 TestMethod 代码来试用它。只需将第一个参数替换为您的 requestb.in 或发布 url。还要放入您的 Calendly api 密钥。

    [TestMethod]
    public void CreateCalendlyWebhookSubscription()
    {
        var task = PostCreateWebhookSubscription();
        task.Wait();
        var response = task.Result;
        var body = response.Content.ReadAsStringAsync().Result;
    }

    private static async Task<HttpResponseMessage> PostCreateWebhookSubscription()
    {
        var client = new HttpClient {BaseAddress = new Uri("https://calendly.com")};
        var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/hooks/");
        var keyValues = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("url","https://requestb.in/17ruxqh1"),
            new KeyValuePair<string, string>("events[]","invitee.created"),
            new KeyValuePair<string, string>("events[]","invitee.canceled")
        };
        request.Content = new FormUrlEncodedContent(keyValues);
        request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") {CharSet = "UTF-8"};
        request.Content.Headers.Add("X-TOKEN", "<your Calendly ApiKey>");
        return await client.SendAsync(request);
    }

希望这对某人有帮助!

于 2018-02-20T05:06:41.973 回答