使用YouTube API v3删除视频

时间:2015-09-15 12:14:36

标签: video drupal youtube youtube-data-api

error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once 'google-api-php-client/src/Google/autoload.php';
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/YouTube.php';
session_start();


/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */

$OAUTH2_CLIENT_ID = 'XXXXXX';//AC
$OAUTH2_CLIENT_SECRET = 'XXXXXXXX';//AC

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('http://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
//print_r($client);
//exit;
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_Youtube($client);
if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
    $youtube->videos->delete( $videoid );
    //echo 'deleting...';
}else if($client->isAccessTokenExpired()) {
    $state = mt_rand();
    $client->setState($state);
    $_SESSION['state'] = $state;
    $authUrl = $client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

} else {
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;
  $authUrl = $client->createAuthUrl();
  ?>
  <h3>Authorization Required</h3>
  <p>You need to <a href="<?php echo $authUrl;?>">authorize access</a> before proceeding.<p>

}

我已经编写了上面的代码,用于从普通的PHP文件中删除我帐户中的YouTube视频。它工作正常。当我在drupal中实现相同时,它给我302找到状态并终止ajax http请求。

$element['remove_button'] = array(
  '#type' => 'button',
  '#value' => t('Remove Video'),
  '#id' => 'youtube-button-remove',
  '#attributes' => array('class' => array('youtube_video_uploader_option')),
  '#ajax' => array(
    'callback' => 'youtube_video_uploader_ajax_remove',
  ),
);

删除视频按钮,我们调用ajax回调函数 - youtube_video_uploader_ajax_remove

function youtube_video_uploader_ajax_remove($form, $form_state) {
 $node=$form['nid']['#value'];
 // $_SESSION['videoid']=$node;
 $commands[] = ajax_command_invoke("#youtube-story-id", "val", array(''));
 $commands[] = ajax_command_invoke("#youtube_preview_launcher", "html", array(''));
 youtube_video_delete_video($node);
 print ajax_render($commands);
 exit;
 }
 function youtube_video_delete_video($nid)
 {
 global $base_url;

 $video_node=node_load($nid);
 //print_r($video_node);

if($video_node->type=='child_profile_videos')
{
    $videoid=$video_node->field_child_profile_video['und'][0]['Video'];
}
if($video_node->type=='youtube_videos')
{
    $videoid=$video_node->field_video_['und'][0]['Video'];
}

if($video_node->type=='programs_videos')
{
    $videoid=$video_node->field_program_video['und'][0]['Video'];
}
$options = youtube_video_uploader_options();


error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once 'google-api-php-client/src/Google/autoload.php';
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/YouTube.php';
session_start();


/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID = 'XXXXXXX';//AC
$OAUTH2_CLIENT_SECRET = 'XXXXXXXX';//AC

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('http://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
//print_r($client);
//exit;
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_Youtube($client);
if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
    $youtube->videos->delete( $videoid );
    //echo 'deleting...';
}else if($client->isAccessTokenExpired()) {
    $state = mt_rand();
    $client->setState($state);
    $_SESSION['state'] = $state;
    $authUrl = $client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

} else {
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;
  $authUrl = $client->createAuthUrl();
  ?>
  <h3>Authorization Required</h3>
  <p>You need to <a href="<?php echo $authUrl;?>">authorize access</a> before proceeding.<p>
}

}

这是ajax回拨功能,依次调用删除视频Youtube API删除视频

你可以帮我解决这个错误吗?

0 个答案:

没有答案