首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用YouTube接口上传视频到多个频道

使用YouTube接口上传视频到多个频道
EN

Stack Overflow用户
提问于 2016-12-07 07:59:41
回答 1查看 516关注 0票数 2

使用YouTube接口将视频上传到单个频道非常简单。这就是我目前使用PHP客户端库的方法:

代码语言:javascript
运行
AI代码解释
复制
<?php

/**
 * This sample adds new tags to a YouTube video by:
 *
 * 1. Retrieving the video resource by calling the "youtube.videos.list" method
 *    and setting the "id" parameter
 * 2. Appending new tags to the video resource's snippet.tags[] list
 * 3. Updating the video resource by calling the youtube.videos.update method.
 *
 * @author Ibrahim Ulukaya
*/

/**
 * Library Requirements
 *
 * 1. Install composer (https://getcomposer.org)
 * 2. On the command line, change to this directory (api-samples/php)
 * 3. Require the google/apiclient library
 *    $ composer require google/apiclient:~2.0
 */
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
}

require_once __DIR__ . '/vendor/autoload.php';
session_start();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
 * 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 = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// 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($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION[$tokenSessionKey] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION[$tokenSessionKey])) {
  $client->setAccessToken($_SESSION[$tokenSessionKey]);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
  $htmlBody = '';
  try{

    // REPLACE this value with the video ID of the video being updated.
    $videoId = "VIDEO_ID";

    // Call the API's videos.list method to retrieve the video resource.
    $listResponse = $youtube->videos->listVideos("snippet",
        array('id' => $videoId));

    // If $listResponse is empty, the specified video was not found.
    if (empty($listResponse)) {
      $htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
    } else {
      // Since the request specified a video ID, the response only
      // contains one video resource.
      $video = $listResponse[0];
      $videoSnippet = $video['snippet'];
      $tags = $videoSnippet['tags'];

      // Preserve any tags already associated with the video. If the video does
      // not have any tags, create a new list. Replace the values "tag1" and
      // "tag2" with the new tags you want to associate with the video.
      if (is_null($tags)) {
        $tags = array("tag1", "tag2");
      } else {
        array_push($tags, "tag1", "tag2");
      }

      // Set the tags array for the video snippet
      $videoSnippet['tags'] = $tags;

      // Update the video resource by calling the videos.update() method.
      $updateResponse = $youtube->videos->update("snippet", $video);

      $responseTags = $updateResponse['snippet']['tags'];

      $htmlBody .= "<h3>Video Updated</h3><ul>";
      $htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>',
          array_pop($responseTags), array_pop($responseTags),
          $videoId, $video['snippet']['title']);

      $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();
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
  $htmlBody = <<<END
  <h3>Client Credentials Required</h3>
  <p>
    You need to set <code>\$OAUTH2_CLIENT_ID</code> and
    <code>\$OAUTH2_CLIENT_ID</code> before proceeding.
  <p>
END;
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>Video Updated</title>
</head>
<body>
  <?=$htmlBody?>
</body>
</html>
update_video.php

脚本第一次运行时,系统会提示我对请求进行身份验证,系统会授予令牌并在60分钟后过期。

我想使用相同的脚本上传视频到不同的渠道,我自己。例如,如果我有3个不同的通道,我可以登录到不同的浏览器来验证应用程序。我目前有10多个YouTube频道,每隔几个小时就想上传一次内容。

有没有一种方法可以从中央服务管理所有这些通道,从而避免每次添加新通道时都进行身份验证。或者,有没有办法使用我的YouTube登录详细信息来验证YouTube应用程序接口?

EN

回答 1

Stack Overflow用户

发布于 2016-12-07 08:12:35

Google与其他YouTube API略有不同。通常,身份验证是基于用户帐户的。因此,如果我授权您访问我的Google日历,您就可以访问我所有的Google日历。

YouTube应用程序接口是基于用户通道的。您必须为每个通道对应用程序进行一次身份验证。然后确保您有一个刷新令牌。将刷新令牌存储在与通道名称相关联的某个位置。然后,您的自动脚本将能够使用刷新令牌来请求新的访问令牌,并在需要时访问您的帐户。

这个问题是针对Analytics api的,但代码应该也适用于YouTube,只需做一些小改动即可指向YouTube API。How to refresh token with Google API client?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41012141

复制
相关文章
关联容器
之前介绍过标准库中的顺序容器,顺序容器是元素在内存中按照一定顺序进行排列的,都是按线性结构进行排列。除了顺序容器外,c++中还有关联容器。与顺序容器不同的是,关联容器中元素是按照关键字来保存和访问的。与之相对的顺序容器是按它们在容器中的位置来顺序的保存和访问的。
Masimaro
2021/05/18
7260
关联容器小结
关联容器和顺序容器的根本不同之处在于,关联容器中的元素是按关键字来保存和访问的(比如map和set),而顺序容器中的元素是按照在容器中的位置来顺序保存和访问的(比如vector和string)。
Enterprise_
2019/11/12
4800
画出最佳分组的生存曲线
做生存分析,Best separation和Median separation,后者很简单,很想学前者,这次带来的是最佳分组的曲线代码。
生信喵实验柴
2023/09/06
3600
画出最佳分组的生存曲线
第 11 章 关联容器
第 11 章 关联容器 标签: C++Primer 学习记录 关联容器 ---- 第 11 章 关联容器 11.1 使用关联容器 11.2 关联容器概述 11.3 关联容器操作 11.4 无序容器 ---- 11.1 使用关联容器 标准库中定义了 8个关联容器,这些容器的不同体现在三个维度上。 或者是一个 set,或者是一个 map。 或者要求不重复的关键字,或者允许重复关键字,允许重复的容器的名字中都包含单词 multi。 或者按顺序保存元素或无序保存。不保持关键字按顺序存储的容器的名字都以 unord
用户1653704
2018/06/07
5680
关联式容器set和map
序列式容器:vector,list,deque,forward_lsit都是序列式容器,因为它们的底层都是线性序列的数据结构,存放的是元素本身。
始终学不会
2023/10/17
2250
关联式容器set和map
【STL】关联容器 — hash_set
容器hash_set是以hash table为底层机制的,差点儿所有的操作都是转调用hash table提供的接口。因为插入无法存储同样的键值,所以hash_set的插入操作所有都使用hash table的insert_unique接口,代码例如以下:
全栈程序员站长
2022/07/13
1880
【STL】关联容器 — hash_set
[入门] Docker将nginx容器和php容器关联起来
首先是在菜鸟教程里看的教程,里面把各种镜像、容器的概念和基本操作都说了。但是每一步都直到怎么测试运行起来。
宣言言言
2019/12/15
3.2K0
STL关联容器-红黑树
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haluoluo211/article/details/80877064
bear_fish
2018/09/14
5460
STL关联容器-红黑树
寻找生存分析的最佳基因表达分组阈值
文字版是: Based on the FPKM value of each gene, we classified the patients into two groups and examined
生信技能树
2019/05/17
1.5K0
规模化运行容器时的最佳数据存储路径
K8s和其他容器编排平台正在迅速下沉到主流的基础设置中,对于大多数面向业务的应用,从传统的数据中心迁移到容器部署还算独立和简单。然而,当遇到需要像数据库或快速数据分析工作负载这样要求更高的核心应用时,事情不那么简单了。
灵雀云
2022/06/06
5700
规模化运行容器时的最佳数据存储路径
数据分组
数据分组就是根据一个或多个键(可以是函数、数组或df列名)将数据分成若干组,然后对分组后的数据分别进行汇总计算,并将汇总计算后的结果合并,被用作汇总计算的函数称为就聚合函数。 Python中对数据分组利用的是 groupby() 方法,类似于sql中的 groupby。 1.分组键是列名 分组键是列名时直接将某一列或多列的列名传给 groupby() 方法,groupby() 方法就会按照这一列或多列进行分组。 groupby(): """ 功能: 根据分组键将数据分成
见贤思齊
2020/08/05
4.6K0
STL之关联式容器map(一)
map<K,T> 类模板:定义了一个保存 T 类型对象的 map,每个 T 类型的对象都有一个关联的 K 类型的键。容器内对象的位置是通过比较键决定的,唯一的要求是键必须可以用 less<K> 比或用自己指定的另一个函数对象来替代。
用户9831583
2022/06/16
3880
STL之关联式容器map(一)
C++primer笔记之关联容器
在这一章中,有以下的几点收获: 1、pair类型的使用相当频繁,如果需要定义多个相同的pair类型对象,可考虑利用typedef简化其声明: typedef pair<string, string> A;这样,在后面的使用中就可以直接用A来代替前面繁琐的书写。 2、三种方法创建pair对象: (1)第一种方法:使用函数make_pair() pair<string, string> spair; string first, last; while(cin >> first >> last) {   spai
Linux云计算网络
2018/01/10
6920
C++primer笔记之关联容器
STL之关联式容器map(二)
成员函数 emplace() 和 insert() 返回的 pair 对象提供的指示相同。pair 的成员变量 first 是一个指向插入元素或阻止插入的元素的迭代器;成员变量 second 是个布尔值,如果元素插入成功,second 就为 true。
用户9831583
2022/06/16
5740
STL之关联式容器map(二)
块存储、对象存储、文件存储, 容器存储的最佳方式应该是什么?
容器的无状态临时存储是一个很好的特性。从镜像启动一个容器,修改,停止,然后重新启动一个容器。一个全新的跟镜像一模一样的容器回来了。
焱融科技
2020/02/28
4.6K0
块存储、对象存储、文件存储, 容器存储的最佳方式应该是什么?
VSCode关联Laradock 容器配置PHPCS插件
本文主要记录如何在 VSCode 关联 Laradock 容器,配置和使用容器的 PHP 环境和一些插件,如:phpcs。
coding01
2021/01/18
1.5K0
【说站】mysql分组查询是什么
以上就是mysql分组查询的介绍,希望对大家有所帮助。更多编程基础知识学习:python学习网
很酷的站长
2022/11/23
9760
docker容器入门最佳教程
容器技术是继大数据和云计算之后又一炙手可热的技术,而且未来相当一段时间内都会非常流行。
sunsky
2020/08/20
6910
docker容器入门最佳教程
容器安全最佳实践入门
保证容器安全是一项复杂的任务。这个问题域很广,面对大量的检查清单和最佳实践,你很难确定采用哪个解决方案。所以,如果你要实现容器安全策略,应该从哪里开始呢?
深度学习与Python
2021/01/20
6720
点击加载更多

相似问题

欧几里德距离vs皮尔逊相关性vs余弦相似度?

312

文档间相似性(余弦相似性)

13

文本(余弦)相似性

110

Python,余弦相似与调整余弦相似

11

调整后的余弦相似性不能正常工作

110
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档