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 アカウントを管理するためのドメイン アクセスを許可するよう求めるウィンドウが表示されます。これを行うと、コードが機能するようになります...プレイリストとプレイリスト アイテムを心ゆくまで追加できます。
ただし、OAUTH2_SCOPES が次のようになるように auth.js を変更する場合:
var OAUTH2_SCOPES = [
'https://www.googleapis.com/auth/youtube.readonly'
];
Cookie をクリアして (既に許可したアクセス許可を継承しないようにするには... ブラウザーを閉じて再起動するだけで十分です)、もう一度やり直してください (HTML にアクセスし、認証リンクをクリックします)。アカウントを管理するのではなく、表示する権限のみを付与するよう求めています。その許可を与えた場合、インターフェイスを介してプレイリストを追加しようとすると、プレイリストを作成できないというエラー メッセージが表示されます。
JavaScript を使用しておらず、代わりにサーバー側言語を使用している場合、前述のように、gapi クライアントは非常にスムーズです。ただし、これらのクライアントでの oAuth2 スコープの処理はそれほど透過的ではなく、設計上「貪欲」です (つまり、サービス エンドポイントをオブジェクトに抽象化するため、必要な最も完全なスコープを要求します)。そのエンドポイントでのアクションのいずれか...そのため、リスト呼び出しのみを行うつもりであっても、サービスに更新アクションがあり、クライアントが完全な管理権限を要求する場合)。ただし、クライアント コードを使いたい場合は、これを変更できます。または、スコープに関して細かく制御できる独自の単純化されたクライアントを作成するためのモデルとして使用することもできます。
それは、あなたの基盤となるテクノロジーを知らなくても、私ができる限り徹底的です. 説明が役立つことを願っています!