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

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

我在这篇文章中阅读:Permanent access token with YouTube api?
您可以将临时 token 交换为永久 token ,但是不建议使用此方法,我无法找到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显然不会被视为 secret 。

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

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

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

08-25 21:35
查看更多