YouTube Data APIをJavaScriptで操作する
ここの「Before your start」を読んで、ビデオを参考にして、oauthの設定をする。
https://developers.google.com/youtube/v3/getting-started
- まず管理画面のデザインが変更になっているのでとまどう。(「APIs & auth」->「Resistered apps」->「Web App-project」でたどり着いた)
- WEB ORIGINにhttpとlocalhostを指定する(例:http://localhost)
- REDIRECT URIにlocalhostを指定するのがミソ(例:http://localhost/oauth2callback)
サンプルを取得する
https://developers.google.com/youtube/v3/code_samples/javascript
githubにあるので、それを取得する
https://github.com/youtube/yt-samples-javascript
git clone https://github.com/youtube/yt-samples-javascript.git
auth.jsの4行目の「OAUTH2_CLIENT_ID」を自分のIDに変更する。
var OAUTH2_CLIENT_ID = '999999999999.apps.googleusercontent.com';
ここからは、環境によるが、とりあえず自分はapacheをインストールした。
sudo apt-get install apache2
サンプルを/var/wwwに移動
ブラウザでアクセス
ログイン認証をするためにsearch.htmlに以下のソースを埋め込む必要がある。
auth.jsで認証に失敗したときに、#login-linkのonclickイベントに再認証する関数を埋め込んでくれる。
<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>