Python 请求论坛时持续出现 401 错误

当我尝试向我的开发论坛发出 Python 请求时,我收到状态码 401。我认为我的凭据都正确,为什么会发生这种情况?

import requests

url = "https://bookish-garbanzo-x5wxpxv7xp96396wp-4200.app.github.dev/posts.json"

# 请求头
headers = {
    "Api-Key": "************************************************************",
    "Api-Username": "NateDhaliwal",
    "Content-Type": "application/json"
}
# 请求中要发送的表单数据
payload = {
    "title": "此主题是使用 API 创建的",
    "raw": "你好,这个**主题**是使用 API 创建的!",
    "category": 6
}
# 使用标头和表单数据发出 POST 请求
response = requests.post(url, headers=headers, json=payload)

# 打印响应状态和文本
print("状态码:", response.status_code)

print("响应正文:", response.text)

您是在 GitHub Codespaces 中运行吗?如果您想在经过身份验证的浏览器外部访问此域,则需要将其设为公开。更多信息请参见:Security in GitHub Codespaces - GitHub Docs

3 个赞

太好了,谢谢!

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.