2014-09-23 173 views
2

我正在尝试制作应用以对YouTube视频进行基本搜索。我使用的是YouTube数据API,我去了谷歌开发者的控制台,并为我的域名创建了一个客户端ID。Google API客户端ID未授权

我下载了Google在其示例代码部分中的auth.js和search.js,并将我的客户端ID放入其'客户端ID'的位置,但应用程序无法正常工作。我使用了console.log,似乎我没有越过函数checkAuth。

我错过了什么?这里是一个链接到页面:http://www.vidme.cassandraburnscreative.com/#search

这里是auth.js和search.js一起

var OAUTH2_CLIENT_ID = 'my client ID'; 
var OAUTH2_SCOPES = [ 
    'https://www.googleapis.com/auth/youtube' 
]; 


googleApiClientReady = function() { 
    console.log("googleApiClientReady"); 
    gapi.auth.init(function() { 
    window.setTimeout(checkAuth, 1); 
    console.log("gapi.auth.init"); 
    }); 
} 


function checkAuth() { 
    gapi.auth.authorize({ 
    client_id: OAUTH2_CLIENT_ID, 
    scope: OAUTH2_SCOPES, 
    immediate: true 
    }, handleAuthResult); 
    console.log("checkAuth"); 
} 


function handleAuthResult(authResult) { 
    if (authResult && !authResult.error) { 
    // Authorization was successful. Hide authorization prompts and show 
    // content that should be visible after authorization succeeds. 
    $('.pre-auth').hide(); 
    $('.post-auth').show(); 
    loadAPIClientInterfaces(); 
    console.log("Load Interfaces"); 
    } else { 

    $('#login-link').click(function() { 
     console.log("nope"); 
     gapi.auth.authorize({ 
     client_id: OAUTH2_CLIENT_ID, 
     scope: OAUTH2_SCOPES, 
     immediate: false 
     }, handleAuthResult); 
     console.log("HandleAuthResult"); 
    }); 
    } 
} 

function loadAPIClientInterfaces() { 
    gapi.client.load('youtube', 'v3', function() { 
    handleAPILoaded(); 
    console.log("handleAPILoaded"); 
    }); 
} 

function handleAPILoaded() { 
    $('#search-button').attr('disabled', false); 
} 

function search() { 
    var q = $('#query').val(); 
    var request = gapi.client.youtube.search.list({ 
    q: q, 
    part: 'snippet' 
    }); 

    request.execute(function(response) { 
    var str = JSON.stringify(response.result); 
    $('#search-container').html('<pre>' + str + '</pre>'); 
    }); 
} 

和HTML

<div class="wrapper"> 

<div id="buttons"> 
<p>Search For an Artist:</p> 
     <label> <input id="query" placeholder='+ Add Artist' type="text"/> 
     <button id="search-button" disabled onclick="search()">Search</button> 
     </label> 
</div> 
    <div id="search-container"> 
    </div> 
</div> 

回答

1

你并不需要连接或使用Oauth2使用YouTube API搜索某些内容。 您只需要一个api key

样本例如:

function googleApiClientReady() { 
    var apiKey = 'YOUR_API_KEY'; 

    gapi.client.setApiKey(apiKey); 
    gapi.client.load('youtube', 'v3', function() { 
     isLoad = true; 
    }); 

    request = gapi.client.youtube.search.list({ 
     q: q, 
     part: 'snippet' 
    }); 
    request.execute(function(response) { 
     //your code to here 
    }); 
} 

不要忘了这个文件添加到您的index.html和添加此以下行的后面:

<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script> 

从DOC YouTube API

+0

谢谢这正是我所需要的。我找不到任何地方使用API​​密钥的方法,所以我试图使用客户端ID。 – Cassandra 2014-09-24 01:17:48

相关问题