I want to use django to change the access token of the LINE message API depending on the request.
However, the decorator is not reflected because the handler is declared in the post function.
How can I set the handler?
In order to change the access token dynamically, I must receive a request.
from django.shortcuts import render
from django.http import HttpResponseForbidden, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from linebot import (LineBotApi, WebhookHandler)
from linebot.exceptions import (InvalidSignatureError)
from linebot.models import (
MessageEvent,
TextMessage,
TextSendMessage,
)
import os
from user_app.models import Profile
from django.views import View
class Api(View):
@csrf_exempt
def post(self, request, *args, **kwargs):
YOUR_CHANNEL_ACCESS_TOKEN = "22222222222222" ←Change dynamically on request
YOUR_CHANNEL_SECRET = "11111111111111111111"Change dynamically on request
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
signature = request.META['HTTP_X_LINE_SIGNATURE']
body = request.body.decode('utf-8')
try:
handler.handle(body, signature)
except InvalidSignatureError:
HttpResponseForbidden()
return HttpResponse('OK', status=200)
@handler.add(MessageEvent, message=TextMessage)
def handle_text_message(event):
print("dddd")
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text=event.message.text))
question from:
https://stackoverflow.com/questions/65641983/how-to-make-the-line-message-api-access-token-change-dynamically-on-request 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…