-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoundcloud_server.js
47 lines (42 loc) · 1.42 KB
/
soundcloud_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(function () {
Accounts.oauth.registerService('soundcloud', 2, function(query) {
var accessToken = getAccessToken(query);
var identity = getIdentity(accessToken);
return {
serviceData: {
id: identity.id,
accessToken: accessToken,
email: identity.email,
username: identity.username
},
options: {profile: {name: identity.username}}
};
});
var getAccessToken = function (query) {
var config = Accounts.loginServiceConfiguration.findOne({service: 'soundcloud'});
if (!config)
throw new Accounts.ConfigError("Service not configured");
var result = Meteor.http.post(
"https://api.soundcloud.com/oauth2/token", {headers: {Accept: 'application/json'}, params: {
code: query.code,
grant_type: "authorization_code",
client_id: config.clientId,
client_secret: config.secret,
redirect_uri: Meteor.absoluteUrl("_oauth/soundcloud?close"),
state: query.state
}});
if (result.error) // if the http response was an error
throw result.error;
if (result.data.error) // if the http response was a json object with an error attribute
throw result.data;
return result.data.access_token;
};
var getIdentity = function (accessToken) {
var result = Meteor.http.get(
"https://api.soundcloud.com/me",
{params: {access_token: accessToken}});
if (result.error)
throw result.error;
return result.data;
};
}) ();