Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: 403 Client Error: Forbidden for url

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.

like image 631
nolan Avatar asked Sep 17 '25 05:09

nolan


1 Answers

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"
}
like image 157
Aftab Mansuri Avatar answered Sep 19 '25 15:09

Aftab Mansuri