本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何在没有用户干预的情况下使用 Google Youtube API v3 接收已知帐户的访问令牌?

发布于2021-08-24 16:58     阅读(600)     评论(0)     点赞(25)     收藏(5)


我需要在 php 中构建一个 web 应用程序,允许 AD 用户轻松地将视频链接添加到我们公司帐户的播放列表中。问题是我们不想让员工知道这个账户的密码。

我们最近安装了一个新的互联网过滤框,可将所有 youtube 流量重定向到 Youtube for Education。唯一的问题是,如果教师想要访问他们需要的教育视频,但该视频不在 edu 网站上,则会被学生阻止。解决方案是将该视频添加到我们的 edu 帐户播放列表中,然后该视频将神奇地为学生播放。

我在这篇文章中读到:使用 YouTube api 的永久访问令牌? 您可以将临时令牌交换为永久令牌,但此方法已被弃用,我找不到 OAuth 2.0 允许这样做的地方。

任何帮助将不胜感激!

更新:

我在 google API 的两个不同位置读到服务帐户不可用于 youtube。但是,我还是决定尝试一下,因为它确实支持 OAuth 身份验证。它就像是在执行查询但不返回任何结果。

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '/secretlocation/youtubeAPIServiceKey.p12';


$client = new Google_Client();

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}



// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
  SERVICE_ACCOUNT_NAME,
  array('https://www.googleapis.com/auth/youtube'),
  $key));                             
$youtube = new Google_YoutubeService($client);
$playlistResponse = $youtube->playlists->listPlaylists('id',
    array("mine"=>true));
print_r($playlistResponse);
$_SESSION['token'] = $client->getAccessToken();

在 google api 网站上的沙箱中完成后,它会返回 5 个项目。在这里它只返回这个:

Array ( [kind] => youtube#playlistListResponse [etag] => "dYdUuZbCApIwJ-onuS7iKMSekuo/Db2Wkrhuywa2CiaiO8EVH7ZukZ8" [pageInfo] => Array ( [totalResults] => 0 [resultsPerPage] => 5 ) [items] => Array ( ) ) 

再次更新:

我确认了我已经知道的。我在尝试向我的帐户添加新播放列表时收到未经授权的错误 (401)。

This error is commonly seen if you try to use the OAuth 2.0 Service Account flow. YouTube does not support Service Accounts, and if you attempt to authenticate using a Service Account, you will get this error.

https://developers.google.com/youtube/v3/docs/errors

回到广场一。


解决方案


您需要将 oauth 2.0 用于桌面应用程序:

https://developers.google.com/accounts/docs/OAuth2InstalledApp

从链接:

1:注册应用程序时,您指定该应用程序是已安装的应用程序。这会导致 redirect_uri 参数的值不同。

2:注册时获取的client_id和client_secret嵌入到你的应用源码中。在这种情况下,client_secret 显然不被视为秘密。

3:授权码可以在浏览器的标题栏中返回到您的应用程序或查询字符串中的 http://localhost 端口。

检查:https : //developers.google.com/accounts/docs/OAuth2WebServer

换句话说,您将访问凭证硬编码到帐户并绕过用户交互进行身份验证。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/135848/6a01c921cfaed9d6bacb/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

25 0
收藏该文
已收藏

评论内容:(最多支持255个字符)