php Youtube Data API v3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Youtube Data API v3相关的知识,希望对你有一定的参考价值。
<?php
/*________________________________________________
SUBSCRIBE USER
------------------------------------------------*/
public function subscribe( Request $request) {
$OAUTH2_CLIENT_ID = '328139019763-quomgab8kpgmek3nroe95l2c81p17gn3.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'DisUNZAbJ1OomaCaMW3ziZh1';
$client = new Google_Client();
// $client->setAuthConfig('jsonfile.json');
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
// $client->setRedirectUri($redirect);
$client->setRedirectUri('http://hotvue.herokuapp.com/admin/ytsubscribe') ;
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
// Check if an auth token exists for the required scopes
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
if (strval( $request->session()->get('state') ) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
// $_SESSION[$tokenSessionKey] = $client->getAccessToken();
Session::put($tokenSessionKey, $client->getAccessToken());
// modify this route to where you want to redirect
return redirect()->route('subscription.subscribe');
// header('Location: ' . $redirect);
}
// ============================ Check Session - Process Token ==========================================
// if (isset($_SESSION[$tokenSessionKey])) {
// if ( isset( Session::get( $tokenSessionKey ) ) ) {
if ($request->session()->has($tokenSessionKey)) {
$client->setAccessToken( Session::get( $tokenSessionKey ) );
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
$htmlBody = '';
try {
// This code subscribes the authenticated user to the specified channel.
// Identify the resource being subscribed to by specifying its channel ID
// and kind.
$resourceId = new Google_Service_YouTube_ResourceId();
// SUBSCRIBE TO THIS CHANNEL
$resourceId->setChannelId('UCtVd0c0tGXuTSbU5d8cSBUg'); // Google dev channel
//$resourceId->setChannelId('UCVi2lI40LetxLBKn-rtWC3A'); // FUnimation Channel
$resourceId->setKind('youtube#channel');
// Create a snippet object and set its resource ID.
$subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
$subscriptionSnippet->setResourceId($resourceId);
// Create a subscription request that contains the snippet object.
$subscription = new Google_Service_YouTube_Subscription();
$subscription->setSnippet($subscriptionSnippet);
// Execute the request and return an object containing information
// about the new subscription.
$subscriptionResponse = $youtube->subscriptions->insert('id,snippet',
$subscription, array());
// SUCCESS SUBSCRIPTION
$htmlBody .= "<h3>Successful Subscription</h3><ul>";
$htmlBody .= sprintf('<li>%s (%s)</li>',
$subscriptionResponse['snippet']['title'],
$subscriptionResponse['id']);
// Flush these session variables
$request->session()->forget($tokenSessionKey);
$request->session()->forget('state');
$htmlBody .= '</ul>';
} catch (Google_Service_Exception $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
// $_SESSION[$tokenSessionKey] = $client->getAccessToken();
Session::put($tokenSessionKey, $client->getAccessToken());
} elseif ($OAUTH2_CLIENT_ID != '328139019763-quomgab8kpgmek3nroe95l2c81p17gn3.apps.googleusercontent.com') {
$htmlBody = "OAUTH KEY REQUIRED " ;
// DEFAULT - DISPLAY AUTH URL
} else {
// If the user has not authorized the application, start the OAuth 2.0 flow.
$state = mt_rand();
$client->setState($state);
// $_SESSION['state'] = $state;
Session::put('state', $state);
$authUrl = $client->createAuthUrl();
$htmlBody = '<p><a id='. "sub" . ' href='. "$authUrl" . '>authorize access</a> before proceeding.<p>';
}
return view('admin.subscription.ytsubscription')->with('htmlBody', $htmlBody) ;
}
以上是关于php Youtube Data API v3的主要内容,如果未能解决你的问题,请参考以下文章
如何解决 youtube data api V3 中的 curl 错误 60:ssl 证书问题?
Youtube API v3 data.search 返回相同的值
如何使用 YouTube Data API v3 更改页面结果
使用 YouTube Data API v3 确定 YouTube 频道的上传速率
YouTube Data API V3:获取多个 videoCategoryId 视频
YouTube 频道中的“已发布视频”和“上传”有啥区别?以及如何通过 YouTube Data v3 API 获取它们?