Javascript SDK connect () function not working in chrome
I am trying to simulate my code following the example here: http://connect.soundcloud.com/examples/connecting.html#
It works in firefox but not chrome. In chrome, the soundcloud popup is displayed correctly and I can login (return to sc-connect.html), but then the window won't close. Upon closer inspection, there is a javascript error because window.opener is null. I wonder if this has something to do with the localhost uri? The example from the link above works in both Firefox and Chrome. Any ideas? My code is 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 this:
<!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 to soundcloud for this app: http: // localhost: 3000 / sc-connect.html
source to share
This is actually a weird bug in Chrome caused by installing the SoundCloud app from the Chrome app store. Unusual, I know.
The workaround, instead of using it window.opener
, pushes the oauth token into LocalStorage or SessionStorage and has a window open to listen for the Storage Event .
source to share
It looks like you are missing a pair of closing parentheses in your example code. Other than that, nothing is clearly visible in 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 it helps.
source to share
<!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>
source to share