How to have users 'reconnect' with soundcloud on each page reload?

给你一囗甜甜゛ 提交于 2019-12-06 11:57:53

问题


I'm using the Javascript SDK inside a node.js (Express) App.

Connecting Users works fine, but the connection does not persist between page reloads. Is this the default behaviour, or is the User supposed to stay connected in new requests?

Do I have to use OAuth token authentication and if so, how can this be done with the JS-SDK? Inside the "Permission"-Popup, Users are already logged in with soundlcoud, though. (just have to click the "connect" button each time)


回答1:


Yep, this is the way to do it, officially. :)

For the Next SoundCloud site, we store the token in localStorage (until the user logs out, of course). In each AJAX request to the API from the front end, we put the oauth token in a request header:

jqXHR.setRequestHeader('Authorization', 'OAuth ' + the_oauth_token);



回答2:


Figured I'd share my answer for those who are unsatisfied with the current answers for automated oauth:

Retrieving access_token:

I had to define get and set cookie functions and then I use the functions to set and retrieve a function holding the access token. I'm not going to give these functions for conciseness but you can easily find them with a google search. I then use this line of code to get the SC access token (once the user has authenticated for the first time)

SC.accessToken()

Setting token:

So this is kind of just an elephant in the room in my opinion that for some reason no one has mentioned. How in the **** do you connect w/ SC using the access token? Do you set it as oauth param? On each call pass it? Well, after experimenting with putting the parameter in every single place I could think, I found out you have to do something like this:

            SC.initialize({
              client_id: '[removed for security reasons]',
              client_secret: '[removed for security reasons]',
              redirect_uri: '[removed for security reasons]',
              access_token: getCookie("sc_lm2"),
              scope: 'non-expiring'
            }); 
//Where "sc_lm2" is the name of my cookie

Hope the helps! Took me a while to figure this out for such a simple thing

EDIT

Using PHP and Wordpress:

$json = wp_remote_get("http://api.soundcloud.com/users/[user_id]/tracks.json?client_id=[client_id]");
$soundcloudData = json_decode($json['body'],true);

(substitue cURL functionality if you're not using Wordpress). @krafty I assume you would just change the endpoint from "/tracks" to "/users" but I can't say I have ever really needed to grab anything but tracks using the Soundcloud API. Hope this helps, though I'm not sure I fully understand what it is that you are trying to accomplish (or rather, how exactly you're going about it) - are you trying to allow user logins? If you want to explain fully what you're trying to accomplish and the steps you're taking I'd be happy to take a crack at it.



来源:https://stackoverflow.com/questions/11116532/how-to-have-users-reconnect-with-soundcloud-on-each-page-reload

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!