Как получить ID Youtube канала при авторизации через google oauth?

Всем привет.

Делаю следующее: отправляю запрос

$params = array(
                'client_id' => Cms::setup('google_key'),
                'redirect_uri' => Cms::setup('home') . '/' . Base::locale() . '/oauth/google',
                'response_type' => 'code',
                'access_type' => 'offline',
                'prompt' => 'consent',
                'flowName' => 'GeneralOAuthFlow',
                'include_granted_scopes' => 'true',
                'gsiwebsdk' => '2',
                'openid.realm' => '',
                'scope' => 'openid profile email https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/yt-analytics.readonly',
            );
            $url = 'https://accounts.google.com/o/oauth2/auth?' . urldecode(http_build_query($params));


https://accounts.google.com/o/oauth2/auth?client_id=МОЙ_КЛИЕНТ_ИД&redirect_uri=СТРАНИЦА_РЕДИРЕКТА&response_type=code&access_type=offline&prompt=consent&flowName=GeneralOAuthFlow&include_granted_scopes=true&gsiwebsdk=2&openid.realm&scope=openid%20profile%20email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics.readonly


На странице редиректа отправляю запрос сюда https://www.googleapis.com/oauth2/v1/userinfo?КОД и получаю данные пользователя. Но как вместо обычных данных пользователя получить данные его ютуб канала или хотя бы ID его канала?

$params = array(
                    'access_token' => $data['access_token'],
                    'id_token' => $data['id_token'],
                    'token_type' => 'Bearer',
                    'expires_in' => 3599,
                    'part' => 'statistics',
                );

                $info = file_get_contents('https://www.googleapis.com/oauth2/v3/userinfo?' . urldecode(http_build_query($params)));
                $info = json_decode($info, true);

                return $info;


В данный момент получаю только эти данные:
Array ( [sub] => 100946860194638725877 [name] => VitaGAME Channel [given_name] => VitaGAME Channel [picture] => https://lh3.googleusercontent.com/a-/AOh14GhDhekDDi89-1CxfTn_l1m5LiTJny9VvLtobGkA=s96-c [email] => igrovoj-kanal-o-6897@pages.plusgoogle.com [email_verified] => 1 [locale] => ru )
  • Вопрос задан
  • 220 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Maiso Автор вопроса
Решение:
$curl = curl_init("https://youtube.googleapis.com/youtube/v3/channels?mine=true&key=" . Cms::setup('google_key'));
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // Disables SSL verification
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'Accept: application/json',
                    'Authorization: Bearer ' . $data['access_token']
                ));
                $user = curl_exec($curl);
                $user = json_decode($user, true);


А когда есть ID канала можно получать дальше различную информацию, отправляя запросы:
$info = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id=' . Cms::Input($channel) . '&key=' . Cms::setup('googlecloud'));
        $info = json_decode($info, true);

$info = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=brandingSettings&id=' . Cms::Input($channel) . '&key=' . Cms::setup('googlecloud'));
        $info = json_decode($info, true);

$info = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=snippet&id=' . Cms::Input($channel) . '&key=' . Cms::setup('googlecloud'));
        $info = json_decode($info, true);


А так получаю данные о последнем видео на канале:
$xml = simplexml_load_file(sprintf('https://www.youtube.com/feeds/videos.xml?channel_id=%s', $channel));

        if (!empty($xml->entry[0]->children('yt', true)->videoId[0])) {
            $date = $xml->entry[0]->published;
            $name = $xml->entry[0]->title;
            $id = $xml->entry[0]->children('yt', true)->videoId[0];
        }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы