axios 我正在尝试设置一个webhook,以便在用户提交Calendly约会后立即检索实时数据

yeotifhr  于 2023-02-04  发布在  iOS
关注(0)|答案(1)|浏览(147)

我正在尝试设置Calendly webhook,以便在用户提交Calendly约会后立即检索实时数据
这是我的nodejs express服务器代码

const port = process.env.PORT || 5000;
const express = require('express');
const axios = require('axios');

const app = express();

const API_KEY = 'MY_API_KEY';
const TOKEN = 'MY_TOKEN';

app.post('/calendly-webhook', (req, res) => {
  // Retrieve real-time data from the request payload
  const data = req.body;
  
  // Log the data to the console
  console.log('Calendly webhook data:', data);
  
  res.send('Webhook received');
});

app.listen(port, () => {
  console.log('Server listening on port 5000');
  
  // Create a new webhook
  axios({
    method: 'post',
    url: 'https://api.calendly.com/webhooks',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'X-Token-Auth': TOKEN
    },
    data: {
      url: 'http://localhost:5000/calendly-webhook',
      events: ['invitee.created']
    }
  })
  .then(response => {
    console.log('Webhook created:', response.data);
  })
  .catch(error => {
    console.error('Webhook creation failed:', error.response.data);
  });
});

我收到此错误

] Webhook creation failed: {
[0]   title: 'Resource Not Found',
[0]   message: 'The requested path does not exist'

我真的不知道我做错了什么而且我对这个还很陌生

ubby3x7f

ubby3x7f1#

您调用API端点创建webhook订阅不正确,请参阅文档:Create Webhook Subscription。请求路径必须不同,并且您缺少一些参数。
正确调用的示例:

axios({
    method: 'post',
    url: 'https://api.calendly.com/webhook_subscriptions',
    headers: {
      'Authorization': `Bearer ${ACCESS_TOKEN}`
    },
    data: {
      url: '<public internet URL>',
      events: ['invitee.created'],
      organization: 'https://api.calendly.com/organizations/AAAAAAAAAAAAAAAA',
      user: 'https://api.calendly.com/users/BBBBBBBBBBBBBBBB',
      scope: 'user'
    }
  })

注意1:不能指定本地主机回调URL。必须指定公共Internet URL。在开发过程中使用RequestBin之类的服务捕获请求,如果要将请求发送到本地计算机,则使用ngrok之类的隧道服务。
注2:在真实的代码中,你可能希望动态获取你正在创建的webhook订阅的组织和用户,例如,你可以调用Get Current User来获取这些信息。
注3:你必须使用OAuth Access Tokens or Personal Access Tokens来调用Calendly的API的当前版本。旧版API键已被弃用。

相关问题