如何在没有用户干预的情况下使用 Google Youtube API v3 接收已知帐户的访问令牌? [英] How do you recieve an access token without user intervention for a known account using Google Youtube API v3?

查看:37
本文介绍了如何在没有用户干预的情况下使用 Google Youtube API v3 接收已知帐户的访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to build a web application in php that allows an AD user to easily add a video link to a playlist of our company account. The problem is we don't want the employees to know the password of this account.

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

We recently installed a new internet filtering box that redirects all youtube traffic to Youtube for Education. The only problem is that if a teacher wants to visit a video they need for education but it isn't on the edu site, it will be blocked for students. The solution is to add that video to our edu account playlist and the video will then magically work for students.

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

I read in this post: Permanent access token with YouTube api? that you can exchange the temporary token for a permanent one but this method has been deprecated and I cannot find where OAuth 2.0 allows for this.

任何帮助将不胜感激!

更新:

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

I read in two different locations in the google API that a Service Account wasn't usable for youtube. But, I decided to try it anyways since it does support OAuth Authentication. It acts like it is performing the query but doesn't return any results.

// 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 个项目.这里它只返回这个:

When done in the sandbox on the google api website it returns 5 items. Here it only returns this:

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

再次更新:

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

I confirmed what I already knew. I was recieving Unauthorized error (401) when trying to add a new playlist to my account.

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:

You need to use oauth 2.0 for desktop applications:

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

来自链接:

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

1: When registering the application, you specify that the application is a Installed application. This results in a different value for the redirect_uri parameter.

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

2: The client_id and client_secret obtained during registration are embedded in the source code of your application. In this context, the client_secret is obviously not treated as a secret.

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

3: The authorization code can be returned to your application in the title bar of the browser or to an http:// localhost port in the query string.

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

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

in other words, you sort of hardcode the access credentials to an account and bypass the user-interaction for authentication.

这篇关于如何在没有用户干预的情况下使用 Google Youtube API v3 接收已知帐户的访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆