本文介绍了YouTube API v3 和 php 返回“请求未指定任何引用者";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 PHP 代码.

I have this PHP code.

<?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>';
}

如果我没有启用密钥限制,这段代码就可以正常工作.但是,如果我启用密钥限制,它会返回...

If I did not enable Key restriction, this code works fine. But if I enable Key restriction it returns...

该请求未指定任何引用者.请确保客户正在发送引用者或使用 API 控制台删除引用者限制.

如何启用密钥限制并使其生效?

How to enable Key restriction and make it work?

我的第二个测试是...

My 2nd test is...

<?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>';
}

此代码适用于密钥限制,但它要求 ALL 用户使用 oAuth 进行身份验证,只是为了查看播放列表和曲目信息,这对任何访问者都不利.

This code works fine with Key restriction but it required ALL users to authenticate using oAuth just to view the playlist and tracks info which is not good for any visitors at all.

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

The same question. How to enable Key restriction and make it work? (Without require any guest/user/visitor action.)

推荐人:

推荐答案

这对我有用

$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 )并检查了请求标头 - 然后我注意到引用标头丢失.
然后我寻找一种方法将该标头传递给请求,并注意到 Guzzle HTTP 客户端类有一个选项.
我不确定这是否是正确的方法,或者它是否只是一种黑客行为,但它对我有用,并希望对某人有所帮助.

UPDATE ( to describe how I got to the code from above ):
I was receiving the same error response from the api request "The request did not specify any referrer". As the environment was local I've installed Charles Web Proxy ( as described in the repository instructions https://github.com/google/google-api-php-client/blob/master/README.md ) and have checked the request headers - then I've noticed that the referrer header was missing.
Then I looked for a way to pass that header to the request and noticed that the Guzzle HTTP Client Class had an option for that.
I'm not sure if that's the right way or if it's just a hack but it worked for me and hoped to be helpful for someone.

这篇关于YouTube API v3 和 php 返回“请求未指定任何引用者";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:44