I'm writing a program that queries Google Safe Browsing for certain urls. I am getting the following error: 'Error: 403 Client Error: Forbidden for url: https://safebrowsing.googleapis.com/v4/threatMatches:find?key=[API_KEY]'
import requests, json, uuid
API_KEY = '...'
def check_urls(urls):
url = 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=' + API_KEY
payload = {
"client": {
"clientId": str(uuid.uuid4()),
"clientVersion": "1.1"
},
"threatInfo": {
"threatTypes": ["MALWARE", "SOCIAL_ENGINEERING",
"UNWANTED_SOFTWARE"],
"platformTypes": ["ANY_PLATFORM"],
"threatEntryTypes": ["URL"],
"threatEntries": [{"url": url} for url in urls]
}
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(url, headers=headers,
data=json.dumps(payload))
response.raise_for_status() # Raise an exception for non-2xx status codes
data = response.json()
if "matches" in data:
malicious_urls = [match["threat"]["url"] for match in data["matches"]]
return malicious_urls
else:
return []
except requests.exceptions.RequestException as e:
print("Error:", e)
return []
def main():
urls = ['example1.com', 'example2.com']
try:
malicious_urls = check_urls(urls)
except:
print('There was an error with the urls!')
if malicious_urls:
print("Malicious URLs found:")
for url in malicious_urls:
print(url)
else:
print("No malicious URLs found.")
I have double checked that my API Key is correct and that I have the API enabled through the platform. I have searched for previous related answers on stackoverflow but didn't work.
Most likely the API is incorrect or not authorized to access the Safe Browsing API so that the request is forbidden.
I would suggest: create a new API in other project and enable it for Safe Browsing.