我正在使用YouTube API将最新视频从YouTube频道加载到WordPress:
function load_feed() {
$url = add_query_arg(
array(
\'part\' => \'snippet\',
\'channelId\' => \'[YouTube channel ID]\',
\'maxResults\' => 3,
\'order\' => \'date\',
\'type\' => \'video\',
\'key\' => \'[YouTube API key]\'
),
\'https://www.googleapis.com/youtube/v3/search\'
);
$response = wp_remote_get(esc_url_raw($url));
return json_decode(wp_remote_retrieve_body($response), true);
}
在Google开发者的API设置中,一切都很好:
Application restrictions: None
但当我设定时:
Application restrictions: HTTP referrers (web sites)
Website restrictions: www.mysite.com/*
我从API得到以下响应:
{
"error": {
"code": 403,
"message": "Requests from referer https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[Channel ID]&maxResults=3&order=date&type=video&key=[API key] are blocked.",
"errors": [
{
"message": "Requests from referer https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[Channel ID]&maxResults=3&order=date&type=video&key=[API key] are blocked.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
API似乎检测到请求来自
googleapis.com 而不是来自
mysite.com, 在这里,我安装了WP,正在运行上述代码。
为什么会发生此错误,我可以做些什么来修复它?