line-bot-sdk-nodejs icon indicating copy to clipboard operation
line-bot-sdk-nodejs copied to clipboard

Request failed with status code 400

Open EriecTanijaya opened this issue 6 years ago • 12 comments

Hi!

I have problem with the the error when response to user via client.replyMessage function

error logs : error log

EriecTanijaya avatar Feb 25 '20 02:02 EriecTanijaya

Hello there, I saw your log. It seems like server has given your reason but the response field is not detailed so please make the response more clearly as now its data is [Object].

And please don't paste the token in your log. Be careful with your secret key.

xingoxu avatar Feb 25 '20 02:02 xingoxu

i will close this for now, since i can't reproduce the error. I think i will re-open if i get one. Thanks for response @xingoxu !

EriecTanijaya avatar Feb 28 '20 02:02 EriecTanijaya

i got the error message : { message: 'Invalid reply token' }

EriecTanijaya avatar Mar 03 '20 07:03 EriecTanijaya

@EriecTanijaya I think it's not sdk's error so please confirm https://developers.line.biz/en/reference/messaging-api/#error-messages as 'Invalid reply token' means An invalid reply token was used in the reply message.

xingoxu avatar Mar 03 '20 07:03 xingoxu

weird, cause how the replyToken in event object is invalid?

EriecTanijaya avatar Mar 03 '20 08:03 EriecTanijaya

@EriecTanijaya Invalid replyToken may be caused in different cases. Check your replyToken is exactly equals the token you received and be sure that token should be only used once.

And if you believe it is sdk problem, paste your code and the repsonse and the request you sent at here. (Be careful of your private access token, you don't need to paste it here.)

xingoxu avatar Mar 03 '20 08:03 xingoxu

this is the code to reply to user

replyText: function(texts = []) {
    let state = this.group_session.state;
    let time = this.group_session.time;
    texts = Array.isArray(texts) ? texts : [texts];

    if (state !== "idle" && state !== "new") {
        if (time < 15) {
            let reminder = "💡 ";

            if (time < 1) {
                reminder +=
                    "Waktu sudah habis, ketik '/check' untuk lanjutkan proses";
            } else {
                reminder +=
                    "Waktu tersisa " +
                    time +
                    " detik lagi, nanti ketik '/check' untuk lanjutkan proses";
            }

            texts.push(reminder);
        }
    }

    return this.client
        .replyMessage(
            this.event.replyToken,
            texts.map(text => ({
                type: "text",
                text: text.trim()
            }))
        )
        .catch(err => {
            console.log("err di replyText", err.originalError.response.data);
        });
},

the error is not always occuring, it happens rarely though

EriecTanijaya avatar Mar 03 '20 09:03 EriecTanijaya

@EriecTanijaya I see your code but I think there's no big problem.

Because the reply token becomes invalid after a certain period of time, responses should be sent as soon as a message is received. Reply tokens can only be used once. https://developers.line.biz/en/reference/messaging-api/#send-reply-message

Do you response the message immediately?

xingoxu avatar Mar 03 '20 09:03 xingoxu

@xingoxu yes, im response the message immediately, but i think its because of replyMessage with wrong replyToken

EriecTanijaya avatar Mar 03 '20 14:03 EriecTanijaya

@EriecTanijaya do your replyToken is exactly the same as the replyToken in webhook payload?

I can't see your other part of code. If it does, I would like you to see your request sent and the webhook payload. If they're the same, that's not sdk side problem but maybe a server problem.

What I can do is only instruct you to submit a problem. @tkgauri

xingoxu avatar Mar 04 '20 01:03 xingoxu

i think its not the sdk problem, but the way i store the event from req.body.events data to 'this'. I think the the value in 'this' is replaced with other data when the request was made. so, before the this.event.replyToken used, the this.event.replyToken is replaced with other request.

EriecTanijaya avatar Mar 04 '20 01:03 EriecTanijaya

Oh... Then it will be a Javascript problem. Sorry but would you please diagnose your code by yourself? I will close this issue for the moment.

xingoxu avatar Mar 04 '20 01:03 xingoxu