79664231

Date: 2025-06-13 00:26:30
Score: 1.5
Natty:
Report link

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! ๐Ÿ˜Š

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (0.5): I need
  • Whitelisted phrase (-2): I fixed
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): facing a similar issue
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Diogo Paiva