4

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

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

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

任何帮助将不胜感激!

更新:

我在谷歌 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

回到原点。

4

1 回答 1

0

您需要将 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

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

于 2013-12-05T22:00:29.587 回答