0

I am trying to run the following code in Spotify apps but gapi.client is considered undefined. I do not have this problem when running in chrome. Does anyone know why or how this is happening.

<!doctype html>
<html>
  <head>
    <title>Vidify</title>
  </head>

  <body>
    <!--Add a button for the user to click to initiate auth sequence -->
    <button id="authorize-button" style="visibility: hidden">Authorize</button>
    <script type="text/javascript">

      var clientId = '591751286145-ktngvf2s76uiuuevan6mo1fft0kchl8l.apps.googleusercontent.com';

      var apiKey = 'AIzaSyB0mteDV5vDKFR-iAv4Fx4OC2gp1Yhqe9U';

      var scopes = 'https://www.googleapis.com/auth/youtube';

      function handleClientLoad() {
        console.log('API key provided - authorizing client...');
        // Step 2: Reference the API key
        gapi.client.setApiKey(apiKey);
        gapi.auth.init(checkAuth);
      }

      function checkAuth() {
        console.log('entered checkAuth - authorizing...');
        setTimeout(function() { 
            console.log("requesting auth");
            gapi.auth.authorize({ 
                client_id: clientId, 
                scope: scopes, 
                response_type:'token',
                immediate: true 
            }, handleAuthResult); 
        }, 100); 
      }

      function handleAuthResult(authResult) {
        console.log('received authResult');
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
          console.log("auth. successful");
          authorizeButton.style.visibility = 'hidden';
          makeApiCall();
        } else {
          console.log('auth failed.');
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

      function handleAuthClick(event) {
        // Step 3: get authorization to use private data
        console.log('retry');
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;

      }

      // Load the API and make an API call.  Display the results on the screen.
        function makeApiCall() {
            console.log("loaded client");
            gapi.client.setApiKey(apiKey);
            // Step 4: Load the Google+ API
            gapi.client.load('youtube', 'v3', function() {
                console.log('youtube API loaded...');
                // Step 5: Assemble the API request
                var qVar = "Kanye West Amazing"
                // changed. added: type
                var request = gapi.client.youtube.search.list({
                    type: 'video',
                    part: 'id',
                    q: qVar
                });
                // Step 6: Execute the API request
                request.execute(function(resp) {

                    console.log(resp);
                    var vid = document.createElement('vid');
                    vid.value = resp.items[0].id.videoId;
                    console.log(vid.value);
                    var youtube_url = "http://www.youtube.com/watch?v=";
                    youtube_url += vid.value;
                    youtube_url += "&rel=0";
                    window.open(youtube_url);
                });
              });
          }
    </script>

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

  </body>
</html>

Do you think this has something to do with loading external API's or is it just that google API's are just special :)

Thomas
  • 3,348
  • 4
  • 35
  • 49
Aakash282
  • 27
  • 9
  • doesn't the second ` – Thomas Jan 02 '14 at 14:32
  • No the second script is in the correct place. I need to load the js functions before I can call them. – Aakash282 Jan 02 '14 at 15:30
  • ok, yeah, sorry. Was skimming https://developers.google.com/youtube/analytics/v1/sample-application and jumped to conclusions. Oh, I was going to say your problem sounded similar to http://stackoverflow.com/questions/20794725/youtube-google-data-api-gapi-client-undefined?noredirect=1#comment31191345_20794725 but that's because it's yours. – Thomas Jan 02 '14 at 16:57
  • repro'd it in a testbed app, out of ideas, but I did find this that sounds similar: https://groups.google.com/forum/#!topic/google-api-javascript-client/VQ3cfPsRUk4 – Thomas Jan 02 '14 at 19:25
  • 1
    The real issue seems to be this: "19:35:27.060 I [https://apis.google.com/js/client.js?onload=makeApiCall:6] Uncaught TypeError: Cannot read property 'prototype' of undefined" I've noticed when I do things like console.log(object) that I only get minimal output in Spotify. I think they've modified the embedded chrome that they use to prevent object inspection, which is preventing client.js from working properly. – Thomas Jan 02 '14 at 19:44
  • I'd still like to know how to do this because I'm currently having to export my api request to another window. I'm sure you understand how annoying that would be for something like a spotify app. – Aakash282 Jan 09 '14 at 05:33
  • Did you by any chance figure out how to fix this? I am seeing a similar issue with HTMLUnit WebClient getPage() with a page that loads the client.js file. – mkhatib Jan 13 '15 at 22:23

0 Answers0