Youtube api示例代码无法运行

时间:2016-09-17 10:34:41

标签: javascript youtube-api

我正在玩google api。此代码来自codeacademy。

这是HTML

<!DOCTYPE html>
<html>
    <head>
        <script src="app.js" type="text/javascript"></script>
        <script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
    </head>
    <body>
        <pre id="response"></pre>
    </body>
</html>

这是javascript:

    // Your use of the YouTube API must comply with the Terms of Service:
    // https://developers.google.com/youtube/terms

    // Helper function to display JavaScript value on HTML page.
    function showResponse(response) {
        var responseString = JSON.stringify(response, '', 2);
        document.getElementById('response').innerHTML += responseString;
    }

    // Called automatically when JavaScript client library is loaded.
    function onClientLoad() {    
        gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
    }

    // Called automatically when YouTube API interface is loaded (see line 9).
    function onYouTubeApiLoad() {

        console.log("onYouTubeApiLoad");
        // This API key is intended for use only in this lesson.
        gapi.client.setApiKey('1111111111AIzaSyBenRS7G9WPDylh2JAXOeD2uLsao6VW2QY');

        search();
    }

    function search() {
        // Use the JavaScript client library to create a search.list() API call.
        var request = gapi.client.youtube.search.list({
            part: 'snippet',
            q: 'surfboards'
        });

        // Send the request to the API server,
        // and invoke onSearchRepsonse() with the response.
        request.execute(onSearchResponse);
    }

    // Called automatically with the response of the YouTube API request.
    function onSearchResponse(response) {
        showResponse(response);
    }

它假设工作正常。但我不知道为什么 - 函数onYouTubeApiLoad() - 永远不会被调用。怎么了?无法弄清楚。

0 个答案:

没有答案