如何使用 Python 运行 Data Explorer 查询

大家好!

关于这个主题:Run Data Explorer queries with the Discourse API - #27

我尝试将 CURL 命令移植到 Python,从语法角度看似乎没有问题,但我总是收到 404 错误,而运行 CURL 命令时却能收到 200。你们能帮我看看我哪里做错了吗?以下是我的 Python 代码:

# 导入

import requests
import json

# 常量

ENDPOINT = 'https://community.myCompanyName.com/admin/plugins/explorer/queries/73/run'
API_KEY = '<the_api_key>'
API_USERNAME = '<my_discourse_username>'

# 核心函数

def send_request(endpoint):

    api_keys = {'Api-Key': API_KEY, 'Api-Username': API_USERNAME}
    headers = {'Content-Type': 'multipart/form-data'}
    request = requests.post(endpoint, api_keys, headers)
    print("状态码:%s" % request.status_code)

send_request(ENDPOINT)

感谢帮助!

3 个赞

您需要将 API 凭据放入请求头中。

headers = {'Content-Type': 'multipart/form-data', 'Api-Key': API_KEY, 'Api-Username': API_USERNAME}

对于实际请求,您可能需要这样指定:

requests.post(endpoint, headers=headers)
6 个赞

太好了,谢谢,这管用!非常感谢 @blake 的帮助!

1 个赞

一篇帖子已合并到现有主题:通过 Node.js 使用 API 创建主题

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