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.
Adding useragent in headers should fix the problem
headers = {
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With