Javascript SDK connect() function not working in c

2019-02-11 02:09发布

问题:

Am attempting to model my code after the example here: http://connect.soundcloud.com/examples/connecting.html#

It works in firefox, but not in chrome. In chrome, the soundcloud popup shows up correctly, and I can sign in (returning to sc-connect.html), but then the window won't close. Upon closer inspection, there's a javascript error because window.opener is null. I wonder if it has to do with localhost uri? The example at the link above works in both firefox and chrome. Any ideas? My code below:

SC.initialize({client_id:'my_client_id', redirect_uri:'http://localhost:3000/sc-connect.html'});

$('button').click(function(){
  SC.connect(function () {
    console.log('made it');
  }
}

My sc-connect.html page looks like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html>
    <head>
      <title>Connect with SoundCloud</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body onload="window.opener.setTimeout(window.opener.SC.connectCallback, 1)">
      <b style="width: 100%; text-align: center;">This popup should automatically close in a few seconds</b>
    </body>
  </html>

Redirect URI on soundcloud for this app: http://localhost:3000/sc-connect.html

回答1:

This is actually a strange bug in Chrome which is caused by installing the SoundCloud app from the Chrome App Store. Bizarre, I know.

A workaround is instead of using window.opener, push the oauth token into LocalStorage or SessionStorage and have the opener window listen to the Storage event.



回答2:

It looks like you're missing a couple of closing brackets in your example code. Other than that, nothing appears obviously wrong with your example. I copied and pasted your sc-connect.html and used it with the following:

<html>
<head>
  <title>Demo</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  <script type="text/javascript" src="http://connect.soundcloud.com/sdk.js"></script>
  <script type="text/javascript">

  $(document).ready(function() {
      SC.initialize({
          client_id: 'MY_CLIENT_ID',
          redirect_uri: 'http://localhost:8080/connect/sc-callback.html'
      });

      $('button').click(function(){
          SC.connect(function() {
              SC.get('/me', function(data) {
                  $('#name').text(data.username);
              });
          });
      });
  });

  </script>
</head>
<body>
  <button>Connect</button>
  <p>
    Hello There, <span id="name"></span>
  </p>
</body>
</html>

This works for me in Firefox and Chrome. Let me know if the that helps.



回答3:

    <!DOCTYPE html> <html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Connect with SoundCloud</title> 

</head>
<body onload="window.opener.setTimeout(window.opener.SC.connectCallback, 1)">
    <b style="text-align: center;">This popup should automatically close in a few seconds</b>
    <script type="text/javascript">window.opener.SC.connectCallback.call(this); </script> 
</body> 
</html> 


标签: soundcloud