0

This is the code to the problem. I am trying to export all the events from mixpanel but I am unable to do so.

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)

I am getting

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>

Can someone help me get out of this trouble?

I was trying to export all the events from mixpanel by calling API. But, am unable to export the events.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

The f string in your headers declaration is in the wrong spot to correctly pass the enc variable:

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

should be:

enc = base64.b64encode(b'apisecret').decode("ascii")
headers = {'Authorization': f'Basic {enc}'}
gbeaven
  • 1,522
  • 2
  • 20
  • 40