Eithar
Eithar

Reputation: 117

API Integration with HIBP

I am trying to write a python script to connect to https://haveibeenpwned.com/ API , to look up a list of email IDs for any breach.

I am starting simple to build on it , but it is not working.

Below is my current code :

import requests
import json

headers = {}
headers['hibp-api-key']='xxx'# key removed 
headers['content-type']= 'application/json'
headers['User-Agent']='testingaccountbreach'
url ='https://haveibeenpwned.com/api/v3/breachedaccount/account'  # email should be URL encoded, email was removed for privacy

response = requests.get(url)
response.status_code

The above code returns 401 server response. Then I tried simple HTTP request still failed, while api integration with virustotal.com worked perfectly with python script , and I can connect to other websites.

url ='https://haveibeenpwned.com/'
response = requests.get(url)
print(response)

I am trying to work on this for over a week now , so I would appreciate any valuable input or guidance .

Regards,

Upvotes: 1

Views: 1642

Answers (1)

Jonothon Starsmore
Jonothon Starsmore

Reputation: 31

So I have been recently working on this too as I am trying to build out an automation system for the company I work for, to leverage Python calls to HIBP and provide information to a document manager. I was having so much trouble getting this to work from scratch with provided Python scripts that people have developed with HIBP (See: https://haveibeenpwned.com/API/Consumers). I then came across your post and code and tried to see if I could get it to work but I kept getting the same 401 like you did. Then I remembered a friend of mine bringing up Postman. If you're not familiar with it, I highly recommend it, because you can setup get requests with API keys and defined URL strings or headers, and output the code across a lot of code languages. It's as simple as plug the data in, and view and copy the code for different languages including Python. I tested it and got a perfect response back in my python script. So I posted below what should work better for you. Be sure the fill in the <> with the proper info.

import requests

url = "https://haveibeenpwned.com/api/v3/breachedaccount/<account>"
hibp_api_key = '<key>'
payload={}
headers = {
  'hibp-api-key': str(hibp_api_key),
  'format': 'application/json',
  'timeout': '2.5',
  'HIBP': str(hibp_api_key),
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

Upvotes: 1

Related Questions