Home>

I'm studying LineBot as a hobby.
I am studying with the book "Let's make an interesting and serious chatbot".

It is a code that returns "hello" for the appropriate word entered on Line.
No matter what I enter, "Hello" is not returned.

There are two things I would like to confirm.
1. There is an output related to [result] in the log shown below. Is the notation of this output able to communicate?
I can't judge if communication is not possible.
2. We use Sakura's rental server as a rental server. this
Can the rental server use the curl function? (I checked it with phpinfo (), but I think it can be used because it is'enable'...)

Input and output information to debug.txt with php.
The following log is output.

[input]
{"events": [{"type": "message", "replyToken": "1dde07c2da904b5d98c3361e8cf47296", "source": {"userId": "U2df22e7ddc417545b5dd8e15c13bf197", "type": "user"}, "timestamp": 1599375 , "mode": "active", "message": {"type": "text", "id": "12630624461427", "text": "d"}}], "destination": "U3f0a01386451f3bfcee30416af01b3cb"}
[output]
{"replyToken": "1dde07c2da904b5d98c3361e8cf47296", "messages": [{"type": "text", "text": "hello"}]}
[result]
{"message": "Authorization header required. Must follow the scheme,'Authorization: Bearer '"}
Corresponding source code
<? php
// Channel access token
define ('TOKEN','I put the LINR channel access token here');
// Delete log file
if (file_exists (DEBUG)) unlink (DEBUG);
// Add to log file
function debug ($title, $text) {
    file_put_contents (DEBUG,'['. $Title.']'. "\ N". $Text. "\ n \ n", FILE_APPEND);
}
function post ($url, $object) {
    // Convert to JSON format
    $json = json_encode ($object);
    debug ('output', $json);
    // Ready to send
    $curl = curl_init ('https://api.line.me/v2/bot/message/'. $url);
    curl_setopt ($curl. CURLOPT_POST, true);
    curl_setopt ($curl. CURLOPT_POSTFIELDS, $json);
    curl_setopt ($curl. CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer'. TOKEN
    ]);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
    // Execute transmission
    $result = curl_exec ($curl);
    debug ('result', $result);
    // End of transmission
    curl_close ($curl);
}
function reply ($event, $text) {
    // Create transmission data
    $object = [
        'replyToken' =>$event->replyToken,
        'messages' =>[['type'=>' text',' text'=>$text]]
    ];;
    // Send
    post ('reply', $object);
}
 ?>
What I tried

Confirm that there is no error in the source code specified by the webhook URL from the LINE developper page (Verify is'success')
Comment out curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);and make sure the [result] output is 1.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    (translate.google.co.jp) Authorization header required. Must follow the scheme

    Approval header required. Must follow the scheme

    So let's doubt the header at the time of request.

    The OAuth 2.0 Authorization Framework: Bearer Token Usage (Japanese)-2.1. Authorization Request Header Field

    credentials = "Bearer" 1 * SP b64token
    Is defined as. One space is required between "Bearer" and b64token.

    There is no single-byte space.

    'Authorization: Bearer'. TOKEN

  • Answer # 2

    [result]
    {"message": "Authorization header required. Must follow the scheme,'Authorization: Bearer '"}

    As you can see, the access token cannot be specified. In the source'Authorization: Bearer'. TOKENIs specified,BearerThere must be a space between the and the token.

    //'Authorization: Bearer'.TOKEN // Wrong
    'Authorization: Bearer'.TOKEN // Positive

  • Answer # 3

    I got stuck by changing the. After $cur1 to.

    curl_setopt ($curl. CURLOPT_POST, true);
    curl_setopt ($curl. CURLOPT_POSTFIELDS, $json);
    curl_setopt ($curl. CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer'. TOKEN
    ]);

    curl_setopt ($curl, CURLOPT_POST, true);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt ($curl, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer'. TOKEN
    ]);