【问题标题】:Code 204 - Error while creating Webhook URL for twitter account in php代码 204 - 在 php 中为 twitter 帐户创建 Webhook URL 时出错
【发布时间】:2020-12-11 21:58:15
【问题描述】:

我正在使用 Twitter 帐户活动 API 发送/接收消息。在其中,我在生成 webhook 时遇到了问题。第一次创建成功。在 webhook 文件中,我在每次发送或接收消息时将消息保存在数据库中。但是当我发送消息时,数据库中没有任何内容。这是 webhook 文件:

const APP_CONSUMER_SECRET = '**********';
// Example token provided by incoming GET request
if(isset($_REQUEST['crc_token'])) {
    $token = $_REQUEST['crc_token'];

    /**
     * Creates a HMAC SHA-256 hash created from the app TOKEN and
     * your app Consumer Secret.
     * @param  token  the token provided by the incoming GET request
     * @return string
     */
    function get_challenge_response($token) {
      $hash = hash_hmac('sha256', $token, APP_CONSUMER_SECRET, true);
      $response = array(
        'response_token' => 'sha256=' . base64_encode($hash)
      );
      return json_encode($response);
    }
}else{
    
    $feedData = file_get_contents('php://input');
    $handleData = fopen('twitterDemo.txt', "w" );
    fwrite($handleData,$feedData);
    fclose($handleData);
    $array = json_decode(file_get_contents('php://input'), true);
    if (isset($array['direct_message_events'][0]['type']) && $array['direct_message_events'][0]['type'] == 'message_create' ) {
        include_once ('config.php');
        include_once ('database-queries.php');
        $message = $array['direct_message_events'][0]['message_create']['message_data']['text'];
        $sender = $array['direct_message_events'][0]['message_create']['sender_id'];
        $from = $array['direct_message_events'][0]['message_create']['target']['recipient_id'];
        $message_type = 'incoming';
        $message_status = 'unread';
        $userId = $sender;
        $account_name = 'twitter';
        $image_url = '';
        if(isset($array['direct_message_events'][0]['message_create']['message_data']['attachment'])){
                $image_url = "Not Image";
        }
        
        $data = array('to'=>$from, 'from'=>$sender, 'msg'=>$message,'image_url' =>$image_url);
        insert($data, $account_name, $message_type, $message_status, $conn);
    }
}

我认为可能存在 webhook 问题,所以我删除了现有应用程序并创建新应用程序并使用新名称为其设置开发环境标签。但是当我尝试创建 webhook 时,它给了我错误:

[code] => 214 [message] => Webhook URL 不符合要求。 无效的 CRC 令牌或 json 响应格式。

我不知道现在这里发生了什么。我正在使用这个 api 创建 webhook url

$url = "https://example.com/twitter/webhook.php";
$result = $connection->post("account_activity/all/env_name/webhooks", ["url" => $url]);

谁能帮我解决这个问题。任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: php twitter webhooks


    【解决方案1】:

    我遇到了同样的错误消息。 问题似乎是 我f(isset($_REQUEST['crc_token'])) 不再工作了。虽然设置了令牌。我不知道是什么导致了问题,但是当我让代码获取 $_REQUEST['crc_token'] 并创建并打印它的工作时。

    但我看到您的代码没有调用 get_challenge_response($token) 函数。 我觉得

    print get_challenge_response($token)
    

    有帮助吗?如果 Webhook 被 twitter 停用,您必须启动另一个 CRC 检查: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/subscribe-account-activity/guides/securing-webhooks

    您的应用还可以在需要时通过使用您的 webhook id 发出 PUT 请求来触发 CRC。在部署新代码并重新启动服务之后,在开发 Webhook 应用程序时触发 CRC 很有用。

    【讨论】:

    • 感谢@user7100414 的回复。嗯,这个问题我已经解决了。
    • @SukhpalSingh 解决方案是什么,我得到了与您分享的相同的回复
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 2015-02-03
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    相关资源
    最近更新 更多