Answer the question
In order to leave comments, you need to log in
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
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;
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
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);
$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];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question