我试图使用 Google 的 API 来消耗一个 YouTube feed,并且我已经设法用一个不受限制的 API 密钥让它工作。问题是,这给我留下了 "配额盗窃 "的机会,所以我需要使用 "HTTP referrers "限制来限制我的密钥,(正如Google所建议的那样)。
我已经尝试在API控制面板中添加以下所有内容作为 "网站限制"。
https://*.example.com/*
http://*.example.com/*
https://example.com/*
http://example.com/*
但当我从 "example.com "调用时,我得到以下错误:
(403) 该请求没有指定任何引用者。请确保客户端正在发送referer或使用API控制台删除referer限制。
我如何在请求头中传递referer值?
我使用的是直接从API文档中提取的代码样本,谁能告诉我如何在请求头中传递referer值?
require_once 'google-api-php-client/src/Google/autoload.php'; // or wherever autoload.php is located
$client = new Google_Client();
$client->setApplicationName('TESTING APP NAME');
$client->setDeveloperKey('AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFg');
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
$queryParams = [
'part' => 'snippet',
'id' => 'UCQ3f82p5yb6MiA9O1LZbmEA',
'maxResults' => 10
];
$response = $service->channels->listChannels('id,snippet,contentDetails,statistics', $queryParams);
谁能告诉我如何在请求中传递referer值?
像我8岁时那样解释一下吧,我。) 非常感谢
就像你8岁时...
你告诉妈妈(YouTube),爸爸(你的网站)需要信息。 当你提出请求时,你需要明确地说,谁在要求信息。我也遇到了同样的代码问题,于是决定用curl来包含 CURLOPT_REFERER
.
<?php
$videoId = 'oHg5SJYRHA0';
$apikey = 'INSERT KEY HERE';
$googleApiUrl = 'https://www.googleapis.com/youtube/v3/videos?id=' . $videoId . '&key=' . $apikey . '&part=snippet';
$HitIt = curl_init();
curl_setopt($HitIt, CURLOPT_REFERER, 'INSERT YOUR URL HERE');
curl_setopt($HitIt, CURLOPT_HEADER, 0);
curl_setopt($HitIt, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($HitIt, CURLOPT_URL, $googleApiUrl);
curl_setopt($HitIt, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($HitIt, CURLOPT_VERBOSE, 0);
curl_setopt($HitIt, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($HitIt);
curl_close($HitIt);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
$title = $value['items'][0]['snippet']['title'];
$description = $value['items'][0]['snippet']['description'];
?>
<h2><?php echo $title; ?></h2>
<p><u>Description</u>: <?php echo $description; ?></p>