前提・実現したいこと
●YouTube Data API の javascript の search.html サンプルを動作させる。
https://developers.google.com/youtube/v3/code_samples/javascript?hl=ja
クロスドメイン対応のためkeyクエリではなくOAuth2.0のAPIを利用してデータを取得したいです。
google cloud console でクライアントIDの取得は済ませており、上記サンプルファイルに値をセット済みです。
また「OAuth 2.0 クライアント ID」の設定画面に関して
承認済みの JavaScript 生成元 => http://localhost
承認済みのリダイレクト URI => 未設定
としています。
発生している問題・エラーメッセージ
ローカルホスト
http://localhost:8080/search.html
にて実行したところ、実行することができません。
該当のソースコード
追記した箇所は auth.jsです
javascript
1 2/ The client ID is obtained from the {{ Google Cloud Console }} 3// at {{ https://cloud.google.com/console }}. 4// If you run this code from a server other than http://localhost, 5// you need to register your own client ID. 6var OAUTH2_CLIENT_ID = 'ここにクライアントIDをセット'; 7var OAUTH2_SCOPES = [ 8 'https://www.googleapis.com/auth/youtube' 9]; 10 11// Upon loading, the Google APIs JS client automatically invokes this callback. 12googleApiClientReady = function() { 13 gapi.auth.init(function() { 14 window.setTimeout(checkAuth, 1); 15 }); 16} 17 18// Attempt the immediate OAuth 2.0 client flow as soon as the page loads. 19// If the currently logged-in Google Account has previously authorized 20// the client specified as the OAUTH2_CLIENT_ID, then the authorization 21// succeeds with no user intervention. Otherwise, it fails and the 22// user interface that prompts for authorization needs to display. 23function checkAuth() { 24 gapi.auth.authorize({ 25 client_id: OAUTH2_CLIENT_ID, 26 scope: OAUTH2_SCOPES, 27 immediate: true 28 }, handleAuthResult); 29} 30 31// Handle the result of a gapi.auth.authorize() call. 32function handleAuthResult(authResult) { 33 if (authResult && !authResult.error) { 34 // Authorization was successful. Hide authorization prompts and show 35 // content that should be visible after authorization succeeds. 36 $('.pre-auth').hide(); 37 $('.post-auth').show(); 38 loadAPIClientInterfaces(); 39 } else { 40 // Make the #login-link clickable. Attempt a non-immediate OAuth 2.0 41 // client flow. The current function is called when that flow completes. 42 $('#login-link').click(function() { 43 gapi.auth.authorize({ 44 client_id: OAUTH2_CLIENT_ID, 45 scope: OAUTH2_SCOPES, 46 immediate: false 47 }, handleAuthResult); 48 }); 49 } 50} 51 52// Load the client interfaces for the YouTube Analytics and Data APIs, which 53// are required to use the Google APIs JS client. More info is available at 54// https://developers.google.com/api-client-library/javascript/dev/dev_jscript#loading-the-client-library-and-the-api 55function loadAPIClientInterfaces() { 56 gapi.client.load('youtube', 'v3', function() { 57 handleAPILoaded(); 58 }); 59} 60
上記サンプルコードにconsole.log()を仕込んでいったところ、
checkAuth()内の gapi.auth.authorize のコールバック handleAuthResult() が呼び出されていないようでした。
特にエラーは起きていないです。
回答1件
あなたの回答
tips
プレビュー