PHP YouTube Data API无需宣誓即可获取频道信息

时间:2015-04-18 02:01:22

标签: php youtube youtube-data-api youtube-channels

我想根据用户名或频道ID提取频道信息。此信息来自用于查看该频道的URL。

EX:https://www.youtube.com/user/csga5000,或https://www.youtube.com/channel/some-channel-id

我有这段代码:

$this->load->library('APIs/YouTube');
echo "[";
echo json_encode($this->youtube->getId('channel/UCkklJA9WbtJM5-g21xHI3yA'),false);
echo "\n]";

调用的函数是:

public function getId($url) {
    $id = explode('/', $url);
    echo json_encode($id).',';

    //User's channel
    if ($id[0] === 'user') {
        $response = $this->youtube->channels->listChannels('id,snippet',array(
            'forUsername' => $id[1]
        ));
    }
    //Channel id
    else {
        $response = $this->youtube->channels->listChannels('id,snippet',array(
            'id' => $id[1],
            'maxResults' => 2
        ));
    }

    return $response;
}

这是youtube类的构造函数:

public function __construct()
{
    $this->client = new Google_Client();
    $this->client->setDeveloperKey($this->api_key);
    $this->client->setClientId($this->client_id);
    $this->client->setClientSecret($this->client_secret);
    $this->client->setScopes(array(
        Google_Service_Oauth2::PLUS_LOGIN,
        Google_Service_Oauth2::PLUS_ME,
        Google_Service_Oauth2::USERINFO_EMAIL,
        Google_Service_Oauth2::USERINFO_PROFILE,
        Google_Service_YouTube::YOUTUBE
    ));

    $ci =& get_instance();
    $this->client->setRedirectUri($ci->config->item('base_url').'auth/youtube/');

    $this->oauth = new Google_Service_Oauth2($this->client);

    $this->youtube = new Google_Service_YouTube($this->client);
}

使用' user / csga5000'调用该功能不起作用

打印的结果是:

[
    [
        "channel",
        "UCkklJA9WbtJM5-g21xHI3yA"
    ],
    {
        "etag":"\"IHLB7Mi__JPvvG2zLQWAg8l36UU\/KcPrlZVHCJ9bAKurpGOj1BBEH6g\"",
        "eventId":null,
        "kind":"youtube#channelListResponse",
        "nextPageToken":null,
        "prevPageToken":null,
        "visitorId":null
    }
]

我只想要这样的结果:

https://www.googleapis.com/youtube/v3/channels?part=id%2Csnippet&id=UCkklJA9WbtJM5-g21xHI3yA&key= {YOUR_API_KEY}

https://www.googleapis.com/youtube/v3/channels?part=id%2Csnippet&forUsername=csga5000&key= {YOUR_API_KEY}

您可以在此测试: https://developers.google.com/youtube/v3/docs/channels/list#try-it

使用forUsername csga5000或id UCkkl​​JA9WbtJM5-g21xHI3yA

我找到的所有例子" mine =>真"和oauth加载这些数据,所以谷歌或堆栈溢出似乎没有任何帮助。

1 个答案:

答案 0 :(得分:1)

我只是错过了一些愚蠢的事情,我对此反应感到非常困惑。

我需要这一行:

$response->getItems();