【问题标题】:PHP YouTube Data API get channel information without oathPHP YouTube Data API 获取频道信息无需宣誓
【发布时间】:2015-06-25 01:33:10
【问题描述】:

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

例如: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

对于用户名 csga5000 或 id UCkkl​​JA9WbtJM5-g21xHI3yA

我发现的所有示例都使用“mine => true”和 oauth 来加载这些数据,因此在 google 上没有任何内容,或者堆栈溢出似乎有帮助。

【问题讨论】:

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


    【解决方案1】:

    我只是错过了一些愚蠢的东西,我对这个反应感到非常困惑。

    我需要这条线:

    $response->getItems();
    

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 2015-03-15
      • 1970-01-01
      • 2013-06-02
      • 2022-12-24
      • 2020-02-19
      • 2020-10-22
      • 2019-04-09
      • 2012-12-14
      相关资源
      最近更新 更多