I was having trouble with a YouTube Data API request and kept receiving this error in the response:
{
"error": {
"code": 403,
"message": "Requests from referer <empty> are blocked.",
"errors": [
{
"message": "Requests from referer <empty> are blocked.",
"domain": "global",
"reason": "forbidden"
}
]
}
}
After logging the raw API response and debugging further, I realized the issue was related to the missing Referer
header in my cURL
request.
Since I was using an API key restricted by domain in the Google Cloud Console, I needed to explicitly set the Referer
header to match one of the allowed domains.
Hereโs how I fixed it:
$ch = curl_init();
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => 'YouTube Module/1.0',
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3,
CURLOPT_REFERER => 'http://my-allowed-domain-in-google.com', // Must match one of your allowed domains
CURLOPT_HEADER => true, // Include headers for debugging
];
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
With this change, I started receiving a 200 OK
response, and the API began returning the expected data โ including the items
array.
If you're facing a similar issue and using a domain-restricted API key, make sure to include the correct Referer
header in your request.
Thanks to everyone who helped me along the way โ happy coding! ๐