简体   繁体   English

代码 204 - 在 php 中为 twitter 帐户创建 Webhook URL 时出错

[英]Code 204 - Error while creating Webhook URL for twitter account in php

I am using twitter account activity api's to send/receive messages.我正在使用 Twitter 帐户活动 api 来发送/接收消息。 In it i am facing problem in generating webhook.在其中,我在生成 webhook 时遇到了问题。 First time it was created successfully.第一次创建成功。 In webhook file i am saving messages in database when each time message sent or received.在 webhook 文件中,每次发送或接收消息时,我都将消息保存在数据库中。 But when I sent message nothing goes in database.但是当我发送消息时,数据库中没有任何内容。 Here is webhook file:这是网络钩子文件:

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

I thought there might be webhook problem so i deleted the existing app and create new one and set development environment label for it with new name.我认为可能存在 webhook 问题,因此我删除了现有应用程序并创建了新应用程序并使用新名称为其设置了开发环境标签。 But for it when i tried to create webhook it gives me error:但是当我尝试创建 webhook 时,它给了我错误:

[code] => 214 [message] => Webhook URL does not meet the requirements. [code] => 214 [message] => Webhook URL 不符合要求。 Invalid CRC token or json response format.无效的 CRC 令牌或 json 响应格式。

I dont know whats happening here now.我不知道现在这里发生了什么。 I am using this api to create webhook url我正在使用这个 api 创建 webhook url

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

Can anyone please help me this out.任何人都可以帮我解决这个问题。 Any help will be appreciated.任何帮助将不胜感激。

Thanks!谢谢!

I ran into the same error message.我遇到了同样的错误消息。 The problem seem to be that the i f(isset($_REQUEST['crc_token'])) is not working anymore.问题似乎是 i f(isset($_REQUEST['crc_token']))不再工作。 Though the token is set.虽然令牌已设置。 I don't know what is causing the problem but when I let the code just fetch the $_REQUEST['crc_token'] and create and print the hash its working.我不知道是什么导致了问题,但是当我让代码只获取 $_REQUEST['crc_token'] 并创建并打印其工作的哈希值时。

But I see your code is not calling the get_challenge_response($token) function.但是我看到您的代码没有调用 get_challenge_response($token) 函数。 I think我认为

print get_challenge_response($token)

may help?可能有帮助吗? If the Webhook was deactivated by twitter you have to initiate another CRC check: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/subscribe-account-activity/guides/securing-webhooks如果 Webhook 被 Twitter 停用,您必须启动另一个 CRC 检查: https : //developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/subscribe-account-activity/guides/securing -网络钩子

Your app can also trigger a CRC when needed by making a PUT request with your webhook id.您的应用程序还可以在需要时通过使用您的网络钩子 ID 发出 PUT 请求来触发 CRC。 Triggering a CRC is useful as you develop your webhook application, after deploying new code and restarting your service.在部署新代码并重新启动服务后,在开发 webhook 应用程序时触发 CRC 非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM