我正在尝试使用 YouTube 数据 API V2.0 为我们客户的视频/频道提取数据见解。我有一个开发人员密钥和我的客户生成的令牌,并成功地弄清楚了如何检索该信息。我的问题是,当我的客户使用该应用程序生成 YouTube 令牌时,我们要求访问意味着一切,并能够“管理”他们的帐户。
这是客户的一个主要问题,他们不希望我们拥有这种完全访问权限。有没有办法让生成的令牌只有只读权限?
非常感谢您的帮助!
我正在尝试使用 YouTube 数据 API V2.0 为我们客户的视频/频道提取数据见解。我有一个开发人员密钥和我的客户生成的令牌,并成功地弄清楚了如何检索该信息。我的问题是,当我的客户使用该应用程序生成 YouTube 令牌时,我们要求访问意味着一切,并能够“管理”他们的帐户。
这是客户的一个主要问题,他们不希望我们拥有这种完全访问权限。有没有办法让生成的令牌只有只读权限?
非常感谢您的帮助!
我已成功用作https://googleapis.com/auth/youtube.readonly
范围;如果您在初始 oAuth 流程中只要求该范围(而不是https://googleapis.com/auth/youtube
同时要求,因为这是将覆盖只读范围的管理范围),那么每当尝试需要管理权限的操作时,您将收到 403 错误(插入、上传、更新、删除)。
v3 的 google-api 客户端可以非常顺利地处理这个问题,如果您正在使用它们的话。如果您编写了自己的 oAuth 流控制,只需确保在请求初始令牌时具有唯一的只读范围。
编辑回复评论:要查看此操作(我将使用 javascript 来显示),您可以使用API 文档提供的示例代码创建一个简单的演示。这是一般过程:
1) 在 Google API 控制台中,创建一个“项目”并为该项目授权 YouTube API(在“服务”选项卡下)。此外,为 Web 应用程序创建一个客户端 ID(在 API 访问选项卡下)并将您的域添加为授权的 Javascript 域。
2) 在您的服务器上,创建一个 HTML 文件作为您的界面(在此示例中,它旨在让您创建一个新的播放列表并向其中添加项目)。这是直接来自文档的代码:
<!doctype html>
<html>
<head>
<title>Playlist Updates</title>
</head>
<body>
<div id="login-container" class="pre-auth">This application requires access to your YouTube account.
Please <a href="#" id="login-link">authorize</a> to continue.
</div>
<div id="buttons">
<button id="playlist-button" disabled onclick="createPlaylist()">Create a new Private Playlist</button>
<br>
<label>Current Playlist Id: <input id="playlist-id" value='' type="text"/></label>
<br>
<label>Video Id: <input id="video-id" value='GZG9G5txtaE' type="text"/></label><button onclick="addVideoToPlaylist()">Add to current playlist</button>
</div>
<h3>Playlist: <span id="playlist-title"></span></h3>
<p id="playlist-description"></p>
<div id="playlist-container">
<span id="status">No Videos</span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="auth.js"></script>
<script src="playlist_updates.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
</html>
3) 在同一位置,使用以下代码创建脚本“playlist_updates.js”(同样,直接来自文档):
// Some variables to remember state.
var playlistId, channelId;
// Once the api loads call a function to get the channel information.
function handleAPILoaded() {
enableForm();
}
// Enable a form to create a playlist.
function enableForm() {
$('#playlist-button').attr('disabled', false);
}
// Create a private playlist.
function createPlaylist() {
var request = gapi.client.youtube.playlists.insert({
part: 'snippet,status',
resource: {
snippet: {
title: 'Test Playlist',
description: 'A private playlist created with the YouTube API'
},
status: {
privacyStatus: 'private'
}
}
});
request.execute(function(response) {
var result = response.result;
if (result) {
playlistId = result.id;
$('#playlist-id').val(playlistId);
$('#playlist-title').html(result.snippet.title);
$('#playlist-description').html(result.snippet.description);
} else {
$('#status').html('Could not create playlist');
}
});
}
// Add a video id from a form to a playlist.
function addVideoToPlaylist() {
addToPlaylist($('#video-id').val());
}
// Add a video to a playlist.
function addToPlaylist(id, startPos, endPos) {
var details = {
videoId: id,
kind: 'youtube#video'
}
if (startPos != undefined) {
details['startAt'] = startPos;
}
if (endPos != undefined) {
details['endAt'] = endPos;
}
var request = gapi.client.youtube.playlistItems.insert({
part: 'snippet',
resource: {
snippet: {
playlistId: playlistId,
resourceId: details
}
}
});
request.execute(function(response) {
$('#status').html('<pre>' + JSON.stringify(response.result) + '</pre>');
});
}
最后,创建文件“auth.js”——这是实际执行 oAuth2 流程的代码:
// The client id is obtained from the Google APIs Console at https://code.google.com/apis/console
// If you run access this code from a server other than http://localhost, you need to register
// your own client id.
var OAUTH2_CLIENT_ID = '__YOUR_CLIENT_ID__';
var OAUTH2_SCOPES = [
'https://www.googleapis.com/auth/youtube'
];
// This callback is invoked by the Google APIs JS client automatically when it is loaded.
googleApiClientReady = function() {
gapi.auth.init(function() {
window.setTimeout(checkAuth, 1);
});
}
// Attempt the immediate OAuth 2 client flow as soon as the page is loaded.
// If the currently logged in Google Account has previously authorized OAUTH2_CLIENT_ID, then
// it will succeed with no user intervention. Otherwise, it will fail and the user interface
// to prompt for authorization needs to be displayed.
function checkAuth() {
gapi.auth.authorize({
client_id: OAUTH2_CLIENT_ID,
scope: OAUTH2_SCOPES,
immediate: true
}, handleAuthResult);
}
// Handles the result of a gapi.auth.authorize() call.
function handleAuthResult(authResult) {
if (authResult) {
// Auth was successful; hide the things related to prompting for auth and show the things
// that should be visible after auth succeeds.
$('.pre-auth').hide();
loadAPIClientInterfaces();
} else {
// Make the #login-link clickable, and attempt a non-immediate OAuth 2 client flow.
// The current function will be called when that flow is complete.
$('#login-link').click(function() {
gapi.auth.authorize({
client_id: OAUTH2_CLIENT_ID,
scope: OAUTH2_SCOPES,
immediate: false
}, handleAuthResult);
});
}
}
// Loads the client interface for the YouTube Analytics and Data APIs.
// This is required before using the Google APIs JS client; more info is available at
// http://code.google.com/p/google-api-javascript-client/wiki/GettingStarted#Loading_the_Client
function loadAPIClientInterfaces() {
gapi.client.load('youtube', 'v3', function() {
handleAPILoaded();
});
}
注意那里的 OAUTH2_SCOPES 常量。它设置为允许完全管理访问权限,因此如果您随后在浏览器中访问 html 页面并单击“授权”链接,您应该会看到要求您授予域访问权限以管理您的 YouTube 帐户的窗口。这样做,然后代码就可以正常工作了……您可以将播放列表和播放列表项添加到您的心脏内容中。
但是,如果您修改 auth.js,使 OAUTH2_SCOPES 看起来像这样:
var OAUTH2_SCOPES = [
'https://www.googleapis.com/auth/youtube.readonly'
];
并清除你的cookies(避免继承你已经授予的权限......只需关闭浏览器并重新启动就足够了),然后再试一次(访问HTML,点击授权链接),你会看到这次是要求您仅授予查看帐户而不是管理帐户的权限。如果您授予该权限,那么当您尝试通过界面添加播放列表时,您会收到一条错误消息,提示您无法创建播放列表。
如果您不使用 javascript,而是使用服务器端语言,正如我提到的,gapi 客户端非常流畅。但是,在这些客户端中对 oAuth2 范围的处理并不那么透明,并且它们在设计上是“贪婪”的(因为它将服务端点抽象为对象,它将请求它需要做的最彻底的范围该端点上的任何操作......因此,即使您只打算进行列表调用,如果服务也具有更新操作,客户端也将请求完全管理权限)。但是,如果您想进入客户端代码,则可以对其进行修改——或者您可以将其用作创建您自己的简化客户端的模型,您可以在范围方面进行精细控制。
在不了解您的基础技术的情况下,这几乎是我所能做到的。希望解释有帮助!