使用 python 导出 Mixpanel 中的事件

问题描述 投票:0回答:1

这是问题的代码。我正在尝试从 mixpanel 导出所有事件,但我无法这样做。

import requests
from requests.auth import HTTPBasicAuth
import base64

apisecret = '....'

enc = base64.b64encode(b'apisecret').decode("ascii")
headers = {f'Authorization': 'Basic {enc}'}
event_names = ['','']
event_names_str = ','.join(event_names)

from_date = '2023-07-20'
to_date = '2023-07-31'

api_url = f'https://data.mixpanel.com/api/2.0/export?from_date={from_date}&to_date={to_date}&event={event_names_str}'
response = requests.get(api_url, headers=headers, json={},
                        auth=HTTPBasicAuth(apisecret, ''))

# Process the response as needed
print(response.status_code)
print(response.text)

我得到了

400

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>400 Bad Request</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Bad Request</h1>
<h2>Your client has issued a malformed or illegal request.</h2>
<h2></h2>
</body></html>

有人可以帮我摆脱这个麻烦吗?

我试图通过调用 API 从 mixpanel 导出所有事件。但是,我无法导出事件。

python api events mixpanel 400-bad-request
1个回答
0
投票

传入授权参数的标头并没有按照您的想法进行。更具体地说,您的 f 字符串不正确:

enc = base64.b64encode(b'apisecret').decode("ascii")
headers = {f'Authorization': 'Basic {enc}'}

应该是:

enc = base64.b64encode(b'apisecret').decode("ascii")
headers = {'Authorization': f'Basic {enc}'}
© www.soinside.com 2019 - 2024. All rights reserved.