django - Twiiter、Webhooks 和 Django CRC 检查 --> 'str' 对象没有属性 'xframe_options_exempt'
问题描述
我将 Django 用于我的 web 应用程序,并调整了教程 @ https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/guides/securing-webhooks以在 Python 3 中运行在我看来,我遇到了这个问题
from django.shortcuts import render
from django.http import HttpResponse, HttpRequest
import base64, hashlib, hmac, json
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.clickjacking import xframe_options_exempt
from django.views.decorators.http import require_GET
@csrf_exempt
@xframe_options_exempt
def twitter_webhook(request):
msg = request.GET.get('crc_token')
msg_bytes = msg.encode()
sha256_hash_digest =
hmac.new(b'bEfpTIneaasdf876asd9f87908709asdf76789689as7dfH', msg_bytes, digestmod=hashlib.sha256).digest()
resp = 'sha256=' + str(sha256_hash_digest)
twitter_response = {
'response_token': resp
}
return json.dumps(twitter_response)
“str”对象没有属性“xframe_options_exempt”
使用pycharm,我逐步调试了我的代码,一切正常,返回适当的哈希,直到它被点击劫持中间件捕获。
Request Method: GET
Request URL: http://127.0.0.1:8000/twitter?crc_token=1230983450923485
Django Version: 2.1.4
Exception Type: AttributeError
Exception Value:
'str' object has no attribute 'xframe_options_exempt'
Exception Location:
C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-
packages\django\views\decorators\clickjacking.py in wrapped_view, line 51
Python Executable:
C:\Users\micha\AppData\Local\Programs\Python\Python37\python.exe
Python Version: 3.7.1
Python Path:
['C:\\Users\\micha\\Documents\\Projects\\sinclaire_webhooks',
'C:\\Program Files\\JetBrains\\PyCharm 2018.3.1\\helpers\\pydev',
'C:\\Users\\micha\\Documents\\Projects\\sinclaire_webhooks',
'C:\\Program Files\\JetBrains\\PyCharm '
'2018.3.1\\helpers\\third_party\\thriftpy',
'C:\\Program Files\\JetBrains\\PyCharm 2018.3.1\\helpers\\pydev',
'C:\\Users\\micha\\.PyCharm2018.3\\system\\cythonExtensions',
'C:\\Users\\micha\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\micha\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\micha\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\micha\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\micha\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site- packages',
'C:\\Program Files\\JetBrains\\PyCharm '
'2018.3.1\\helpers\\pycharm_matplotlib_backend']
Server time: Sun, 16 Dec 2018 17:58:20 +0000
我已经搜索过并且找不到任何明确的东西来引导我解决这个问题,并且对于 python 和 django 来说都是半新的,任何帮助将不胜感激!
解决方案
问题是您直接从视图返回一个 JSON 字符串,这会导致xframe_options_exempt
装饰器翻倒,因为它需要一个HttpResponse
. Django 视图函数应该返回一个HttpResponse
.
您可以修改视图以返回HttpResponse
如下内容:
return HttpResponse(json.dumps(twitter_response), content_type='application/json')
或者使用JsonResponse
(的子类HttpResponse
)并让 Django 处理字典到 JSON 的转换:
from django.http.response import JsonResponse
...
@csrf_exempt
@xframe_options_exempt
def twitter_webhook(request):
...
twitter_response = {
'response_token': resp
}
return JsonResponse(twitter_response) # No need to use json.dumps()
推荐阅读
- javascript - 如何在 RegExp 中使用 [^x] 而不匹配 'not x' 的内容?
- python - 导入子文件夹给出错误 no module named
- python - 如何一次将excel文件中的所有Nan值设置为0
- mysql - 寻找 MariaDB 的 COLUMN_ADD 函数的 PHP 或 MySQL 等效项
- jakarta-ee - 如何从对象列表中删除几个对象
- python - 在我的 for 循环中无法识别语法错误
- r - 如何将 na.approx 应用于矩阵中的一行?
- c++ - 是否有一个函数可以确定是否在 c 字符串中找到某个字符?
- php - 容量和性能:$_COOKIE 与 $_SESSION
- python - 这是什么类型的图表以及如何在 Python 中创建它?