【问题标题】:Webhook not valid after <Redirect> in Twilio在 Twilio 中的 <Redirect> 之后 Webhook 无效
【发布时间】:2021-10-19 21:44:05
【问题描述】:

我正在尝试在 twilio 中测试一个工作流程,工作流程是这样的:

  • 有一个电话进来,我的服务器被 twilio 击中,请求根据docs 是有效的
  • webhook 返回一个 Twiml 并重定向到另一个端点,以便将调用加入队列。
  • 此端点上的 webhook 无效。

除此第二个端点外的所有端点都在正确验证。在验证这样的重定向时,我是否需要考虑任何特殊情况?

如果这很重要,我正在使用 laravel。

[编辑1]

twiml 是这样的

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say></Say>
    <Redirect>https://example.com/webhook/call/handle-incoming-call?Enqueue=1</Redirect>
</Response>

重定向到该 url 后,请求返回 403 错误。

为了验证,我在 laravel 中的路由中间件中使用以下代码。

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Response;
use Twilio\Security\RequestValidator;

class TwilioRequestValidator
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Be sure TWILIO_TOKEN is set in your .env file.
        // You can get your authentication token in your twilio console https://www.twilio.com/console
        $requestValidator = new RequestValidator(env('TWILIO_TOKEN'));

        $requestData = $request->toArray();

        // Switch to the body content if this is a JSON request.
        if (array_key_exists('bodySHA256', $requestData)) {
            $requestData = $request->getContent();
        }
        $isValid = false;

        if ($request->hasHeader('X-Twilio-Signature')) {
            $isValid = $requestValidator->validate(
                $request->header('X-Twilio-Signature'),
                $request->fullUrl(),
                $requestData
            );
        }


        if ($isValid) {
            return $next($request);
        } else {
            return new Response('Invalid Request', 403);
        }
    }
}

谢谢。

【问题讨论】:

  • 你能提供更多细节吗?返回重定向的代码是什么样的?您如何验证这两个端点上的 webhook?
  • 我用其他信息更新了问题
  • 当您调用$request-&gt;toArray() 时,它只是返回请求正文中的参数还是包含查询字符串参数? (抱歉,我不是 Laravel 开发人员。)如果它包含查询字符串参数,那么这就是错误的原因,您应该尝试仅获取 POST 请求正文参数(可能是request-&gt;request-&gt;all()?)。
  • 是的,这就是问题所在,我不得不使用 $request->post() 而不是 toArray 方法。谢谢指点

标签: twilio twilio-api twilio-php twilio-twiml


【解决方案1】:

这里是 Twilio 开发者宣传员。

而不是使用返回所有参数的request-&gt;toArray(),您只需要正文参数。查询字符串参数在request-&gt;fullUrl()中已经处理好了。

尝试request-&gt;post() 来获取 POST 数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多