V
V
Vitaly2022-02-26 21:07:26
Google
Vitaly, 2022-02-26 21:07:26

How to get Youtube channel ID when logging in with google oauth?

Hello.

I do the following: send a request

$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


On the redirect page, I send a request here https://www.googleapis.com/oauth2/v1/userinfo?КОДand get the user data. But how to get the data of his YouTube channel or at least the ID of his channel instead of the usual user data?

$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;


At the moment I'm only getting this data:
Array ( [sub] => 100946860194638725877 [name] => VitaGAME Channel [given_name] => VitaGAME Channel [picture] => https://lh3.googleusercontent.com/a-/AOh14GhDhekDDi89-1CxfTn_l1m5LiTJny9VvLtobGkA=s96-c [email] => [email protected] [email_verified] => 1 [locale] => ru )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2022-02-28
@Maiso

Decision:

$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);

And when there is a channel ID, you can get further various information by sending requests:
$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);

And this is how I get data about the last video on the channel:
$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];
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question