Python 3 urllib与Slack B不一致

2024-04-19 14:07:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的python脚本,它应该在Slack上向我发送一条消息,它工作得很好。在

#!/usr/bin/python

from urllib.request import Request, urlopen
import json

# Provide the webhook URL that slack generated
slack_webhook_url = 'https://hooks.slack.com/services/josadfs/nfonwnfoawf/abcdsads'

# Post the message to the slack webhook
message = {
    "text": "Hello world"
}

req = Request(slack_webhook_url, json.dumps(message).encode('utf-8'))

response = urlopen(req)
response.read()

我有另一个剧本,也做同样的事,只是为了不和。在

^{2}$

出于某种原因,这给了我一个HTTP Error 403: Forbidden错误。在

curl命令都可以工作,因此这不是API密钥问题。不过,discord脚本在使用requests模块时可以工作。在

#!/usr/bin/python

import json
import requests

# Provide the webhook URL that Discord generated
discord_webhook_url = 'https://discordapp.com/api/webhooks/252534154132/knskdnvoangoe0940507230'

# Post the message to the Discord webhook
data = {
    "content": "Hello world"
}
requests.post(discord_webhook_url, data=data)

我的urllib实现有什么问题吗?可以使用urllib发送不一致请求吗?在


Tags: theimport脚本jsonurlmessagedatabin