Solved

API Error: 403 Forbidden When Exporting Data

  • 8 February 2023
  • 5 replies
  • 512 views

When exporting data with the Export API through Python requests, I receive a 403 Forbidden error. The API key and secret are correct, and encoded correctly, since when they were incorrect, I received a 401 error that has since resolved.

I am also on the US data center, but I tried both the US and EU centers and received a 403 error for both.

I am using the following code, which is the same request as in the documentation:

def get_api():

headers = {
'Authorization': 'Basic {REDACTED API KEY}',
}

params = {
'start': '20220101T00',
'end': '20220127T00',
}

response = requests.get('https://amplitude.com/api/2/export', params=params, headers=headers)
print(response)
print("Done!")

I have reviewed the following posts with no success.

 

icon

Best answer by jmagg 11 February 2023, 00:24

View original

5 replies

Userlevel 6
Badge +9

Thanks for reaching out @kanta. I’ll make sure the team takes a look!

Userlevel 5
Badge +6

Hi @kanta - thanks for posting! Sounds like you’re receiving a 403 Forbidden error when using the Export API through Python requests. I’m familiar with making calls through Postman, so I’ll do my best to help but may need to call on a team member to jump in. Here’s something to get us started --

 

Here would be an example command call if you called through Terminal:

curl -u api:secret ‘https://amplitude.com/api/2/export?start=20211115T5&end=20211116T20’ >> yournewfilename.zip

Does calling through the Terminal work for you?

 

Would you please try a setup like this example in Python and see if it works please?

import http.clientconn = http.client.HTTPSConnection("amplitude.com")
payload = ''
headers = {
'Authorization': 'Basic BASIC_TOKEN'
}
conn.request("GET", "/api/2/export?start=20210101T0&end=20221231T23", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Hi @jmagg , tried the curl command and also the python script using the http module, and neither way worked -- the curl command output a corrupt zip, and the python script resulted in the same 403 error.

Userlevel 6
Badge +9

Thanks for circling back @kanta We will make sure your reply gets to the support team for review. In the meantime this article might help as you continue to resolve. 

 

Userlevel 5
Badge +6

Hi @kanta -- appreciate you giving my ideas a try!

Here are some additional things we can consider:

  1. Best guess: for the headers portion of your call, what are you inputting for “REDACTED API KEY” in the 'Authorization': 'Basic {REDACTED API KEY}' line? Are you simply inputting your project’s API key? Since it’s basic auth, you’d actually need to input a generated response from the API key & secret key both! You can generate a basic auth token here.
  2. Is your IT team blocking anything that would be interrupting this API call?
  3. Is there a VPN, Ad Blocker, or any 3rd party applications that may be interfering with this API call?
  4. Does data exist in the time range/project you are trying to export?

Reply