YouTube API v3和php返回“请求未指定任何引用”

我有这个PHP代码.

<?php 

require 'vendor/autoload.php';

$youtube_api_key = 'MY_KEY';

$playlist_id = 'PL3BE743993147F061';

$client = new \Google_Client();
$client->setDeveloperKey($youtube_api_key);
$youtube = new \Google_Service_YouTube($client);

try {
    $playlistResponse = $youtube->playlists->listPlaylists('snippet', array(
        'id' => $playlist_id
    ));
    echo '<pre>'.print_r($playlistResponse, true).'</pre>';
} catch (\Google_Service_Exception $e) {
    $gse_errors = $e->getErrors();
    echo '<h1>error!</h1>';
    echo '<pre>'.print_r($gse_errors, true).'</pre>';
}

如果我没有启用密钥限制,此代码可以正常工作.但是,如果我启用密钥限制,它将返回…

The request did not specify any referer. Please ensure that the client
is sending referer or use the API Console to remove the referer
restrictions.

如何启用密钥限制并使其工作?

我的第二个测试是……

<?php 

require 'vendor/autoload.php';

session_start();

$youtube_api_key = 'MY_KEY';
$oauth_client_id = 'MY_CLIENT_ID';
$oauth_client_secret = 'MY_CLIENT_SECRET';

$playlist_id = 'PL3BE743993147F061';

//$client = new \Google_Client();
//$client->setDeveloperKey($youtube_api_key);

$client = new Google_Client();
$client->setClientId($oauth_client_id);
$client->setClientSecret($oauth_client_secret);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

$youtube = new \Google_Service_YouTube($client);

$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION[$tokenSessionKey])) {
  $client->setAccessToken($_SESSION[$tokenSessionKey]);
}


try {
    if ($client->getAccessToken()) {
        $playlistResponse = $youtube->playlists->listPlaylists('snippet', array(
            'id' => $playlist_id
        ));
        echo '<pre>'.print_r($playlistResponse, true).'</pre>';
    } else {
        // If the user hasn't authorized the app, initiate the OAuth flow
        $state = mt_rand();
        $client->setState($state);
        $_SESSION['state'] = $state;

        $authUrl = $client->createAuthUrl();
        $htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
        echo $htmlBody;
    }
} catch (\Google_Service_Exception $e) {
    $gse_errors = $e->getErrors();
    echo '<h1>error!</h1>';
    echo '<pre>'.print_r($gse_errors, true).'</pre>';
}

此代码适用于密钥限制但它要求所有用户使用oAuth进行身份验证只是为了查看播放列表并跟踪对任何访问者都不利的信息.

同样的问题.如何启用密钥限制并使其工作? (无需任何访客/用户/访客操作.)

引用:

> https://developers.google.com/youtube/v3/code_samples/php#set_and_retrieve_localized_playlist_metadata
> https://developers.google.com/youtube/v3/docs/

最佳答案
这对我有用

$referrer = 'my.domain';
$api_key = 'apikey';
$client = new Google_Client();
$client->setDeveloperKey($api_key);

$headers = array('Referer' => $referrer);
$guzzleClient = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), 'headers' => $headers ));        
$client->setHttpClient($guzzleClient);

更新(描述我如何从上面得到代码):
我从api请求收到了相同的错误响应“请求没有指定任何引用者”.由于环境是本地的,我已经安装了Charles Web Proxy(如存储库指令https://github.com/google/google-api-php-client/blob/master/README.md中所述)并检查了请求标头 – 然后我注意到了referrer标头丢失了.
然后我找了一种方法将该标头传递给请求,并注意到Guzzle HTTP Client Class有一个选项.
我不确定这是不是正确的方式,或者它只是一个黑客,但它对我有用,并希望对某人有所帮助.

转载注明原文:YouTube API v3和php返回“请求未指定任何引用” - 代码日志