Q&A

PHP LineBot アクセストークン、シークレットトークンはあっているが返信がこない

現状 line-bot-sdk-phpでオウム返しをするBotを作成しています。メッセージを送信すると返信が帰ってこない状態となっていてデバックで送信されたデータが取れるのかを確認するとデータもしっかり取れています。しかしレスポンスを返す処理をデバックで確認んしたところアクセストークンが認証しないとのエラーメッセージが出ている状態となっています。 ですが、アクセストークンを再度確認したのですが間違っていませんでした。何が原因でアクセストークンが認証されないのかご教授いただけると幸いです。

発生している問題・エラーメッセージ

アクセストークン、シークレットトークンはあっているが返信がこない。アクセストークンが認証されない。また直接関係あるのかはわからないのですが投稿した時刻とレスポンスの時刻がなぜかあっていない状態です。

//送信した内容
LINE\LINEBot\Event\MessageEvent\TextMessage Object
(
    [emojis:LINE\LINEBot\Event\MessageEvent\TextMessage:private] => 
    [mentionees:LINE\LINEBot\Event\MessageEvent\TextMessage:private] => 
    [message:protected] => Array
        (
            [type] => text
            [id] => 13992700300144
            [text] => や
        )

    [event:protected] => Array
        (
            [type] => message
            [message] => Array
                (
                    [type] => text
                    [id] => 13992700300144
                    [text] => や
                )

            [timestamp] => 1620093353424
            [source] => Array
                (
                    [type] => user
                    [userId] => U23d1c181167c4cf898db12e3e6555ac6
                )

            [replyToken] => 80ff35c4c5da441d974ee71350c3ad93
            [mode] => active
        )

)

2021/05/04 10:55:52 //メッセージを送った時間

//レスポンス
LINE\LINEBot\Response Object
(
    [httpStatus:LINE\LINEBot\Response:private] => 401
    [body:LINE\LINEBot\Response:private] => {"message":"Authentication failed. Confirm that the access token in the authorization header is valid."}
    [headers:LINE\LINEBot\Response:private] => Array
        (
            [Server] => openresty
            [Date] => Tue, 04 May 2021 01:55:53 GMT
            [Content-Type] => application/json
            [Transfer-Encoding] => chunked
            [Connection] => keep-alive
            [x-line-request-id] => 058db0b0-02be-4883-b5c8-67e45f141562
            [x-content-type-options] => nosniff
            [x-xss-protection] => 1; mode=block
            [cache-control] => no-cache, no-store, max-age=0, must-revalidate
            [pragma] => no-cache
            [expires] => 0
            [x-frame-options] => DENY
            [www-authenticate] => Bearer error="invalid_token", error_description="invalid token"
        )

該当のソースコード

require_once('vendor/autoload.php');


use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\Constant\HTTPHeader; 
   //先ほど取得したチャネルシークレットとチャネルアクセストークンを以下の変数にセット
$channel_access_token = 'xxxxxx';
$channel_secret = 'xxxxxx';

$http_client = new CurlHTTPClient($channel_access_token);
$bot = new LINEBot($http_client, ['channelSecret' => $channel_secret]);
$signature = $_SERVER['HTTP_' . HTTPHeader::LINE_SIGNATURE];
$request_body = file_get_contents('php://input');
$events = $bot->parseEventRequest($request_body, $signature);
$event = $events[0];
$events = $bot->parseEventRequest($request_body, $signature);

$reply_token = $event->getReplyToken();
$reply_text = $event->getText();
$bot->replyText($reply_token, $reply_text);
error_log(print_r($event, true) . "\n", 3, "debug.log");
error_log( date("Y/m/d H:i:s") . "\n", 3, "debug.log");                                                                                                   
error_log(print_r($bot->replyText($reply_token, $reply_text), true) . "\n", 3, "debug.log");

試したこと

試したこと アクセストークン、シークレットトークンの再発行。 別チャンネルの作成 サーバーを別のものを使用。

補足情報(FW/ツールのバージョンなど)

実装環境 CentOS8 PHP7.3

  • 0
  • 2
  • 3240
  • twitter facebook

確認しましたが、間違ってはおりません。

  • 0

Authentication failed. Confirm that the access token in the authorization header is valid.invalid_tokenとあるので、Channel SecretとAccess Tokenが逆になっていないかやそれぞれのコピーを正しくできているか確認してもらうといいと思います。

  • 0
Are you sure? question.vm