Python 无法通过AWS API网关验证从slack到Lambda的消息

Python 无法通过AWS API网关验证从slack到Lambda的消息,python,aws-lambda,slack-api,Python,Aws Lambda,Slack Api,我试图通过AWS API网关验证slack发出的消息,我传递给Lambda函数的一个例子是 {'method': 'POST', 'body': {'token': 'xxxxxx', 'team_id': 'xxxxxx', 'api_app_id': 'xxxxxx', 'event': {'client_msg_id': 'xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx', 'type': 'message', 'text': 'test', 'user': 'xxx

我试图通过AWS API网关验证slack发出的消息,我传递给Lambda函数的一个例子是

{'method': 'POST', 'body': {'token': 'xxxxxx', 'team_id': 'xxxxxx', 'api_app_id': 'xxxxxx', 'event': {'client_msg_id': 'xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx', 'type': 'message', 'text': 'test', 'user': 'xxxxxx', 'ts': 'xxxxxx.xxxx', 'team': 'xxxxxx', 'blocks': [{'type': 'rich_text', 'block_id': 'xxx', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'text', 'text': 'test'}]}]}], 'channel': 'xxxxxx', 'event_ts': 'xxxxxx.xxxx', 'channel_type': 'im'}, 'type': 'event_callback', 'event_id': 'xxxxxxxxx', 'event_time': 1576188370, 'authed_users': ['xxxxxxx']}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip,deflate', 'Content-Type': 'application/json', 'Host': 'xxxxxxxxxx.execute-api.us-east-1.amazonaws.com', 'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)', 'X-Amzn-Trace-Id': 'Root=1-xxxxxx-xxxxxxxxx', 'X-Forwarded-For': '54.xxx.xxx.xxx', 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https', 'X-Slack-Request-Timestamp': '1576188371', 'X-Slack-Signature': 'v0=xxxxxxxxxxxxxxxxxxxxxxxx'}}
仅摘录正文/请求正文

{'token': 'xxxxxx', 'team_id': 'xxxxxx', 'api_app_id': 'xxxxxx', 'event': {'client_msg_id': 'xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx', 'type': 'message', 'text': 'test', 'user': 'xxxxxx', 'ts': 'xxxxxx.xxxx', 'team': 'xxxxxx', 'blocks': [{'type': 'rich_text', 'block_id': 'xxx', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'text', 'text': 'test'}]}]}], 'channel': 'xxxxxx', 'event_ts': 'xxxxxx.xxxx', 'channel_type': 'im'}, 'type': 'event_callback', 'event_id': 'xxxxxxxxx', 'event_time': 1576188370, 'authed_users': ['xxxxxxx']}
我提取X-Slack-Signature、X-Slack-Request-Timestamp和对象的主体,然后将其传递给以下函数

def verify_slack_request(slack_signature=None, slack_request_timestamp=None, request_body=None):
    slack_signing_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
    print(request_body)
    data_body = json.dumps(request_body)

    basestring = 'v0:' + slack_request_timestamp + ':' + json.dumps(request_body, indent=None)
    slack_signing_secret = bytes(slack_signing_secret, 'utf-8')
    unicode_basestring = bytes(basestring, 'utf-8')
    my_signature = 'v0=' + hmac.new(slack_signing_secret, unicode_basestring, hashlib.sha256).hexdigest()

    print(my_signature)
    print(slack_signature)

我遇到的问题是,随方法传递的slack签名和我的签名没有对齐,我已经能够验证slack slash命令,但常规聊天命令似乎无法使用此方法工作。

问题是
json。dumps
不会删除键和值之间的空格。该函数需要一个
分隔符
参数来删除空格

尝试以下代码,您应该将lambda事件体作为dict(而不是字符串)传递给此函数

import json
import hashlib
import hmac

def create_signature(secret, timestamp, data):

    newdata =json.dumps(data, separators=(',', ':'))

    req = ('v0:' + str(timestamp) + ':' + newdata).encode()
    print('sigBaseString: ', req)
    request_signature= 'v0='+hmac.new(
        str.encode(secret),
        req, hashlib.sha256
    ).hexdigest()

    return request_signature

评论不用于扩展讨论;此对话已结束。谢谢@SamuelLiew,我正在等待聊天选项出现,因为所有参与者都需要20个代表,否则主持人需要手动移动到聊天。