forked from sylvaingi/meteor-soundcloud
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoundcloud_server.js
73 lines (63 loc) · 2.4 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Accounts.oauth.registerService('soundcloud');
// see: http://developers.soundcloud.com/docs/api/reference#me
Accounts.soundcloud.whitelistedFields = [
'id', 'username', 'permalink', 'permalink_url', 'avatar_url', 'country',
'full_name', 'city', 'description', 'website', 'discogs-name', 'myspace-name',
'track_count', 'playlist_count', 'followers_count', 'followings_count',
'public_favorites_count', 'private_tracks_count', 'private_playlists_count'
];
var handleOauthRequest = function(query) {
var accessToken = getAccessToken(query);
var identity = getIdentity(accessToken);
// call user update method here..
var serviceData = {accessToken: accessToken};
var _serviceFields = _.pick(identity, Accounts.soundcloud.whitelistedFields);
_.extend(serviceData, _serviceFields);
var rv = {
serviceData: serviceData,
options: {profile: {name: identity.full_name}}
};
return rv;
};
var getAccessToken = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'soundcloud'});
if (!config)
throw new ServiceConfiguration.ConfigError("Service not configured");
var rv;
try {
rv = 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
}
});
} catch (err) {
throw new Error("Failed to complete OAuth handshake with soundcloud. " + err.message);
}
if (rv.data.error) // if the http rv was a json object with an error attribute
throw new Error("Failed to complete OAuth handshake with soundcloud. " + rv.data.error);
return rv.data.access_token;
};
var getIdentity = function (accessToken) {
try {
var rv = Meteor.http.get("https://api.soundcloud.com/me", {
params: {
oauth_token: accessToken,
format: "json"
}
});
// console.info("fetching identity from: https://api.soundcloud.com/me", rv);
return rv.data;
} catch (err) {
throw new Error("Failed to fetch identity from soundcloud. " + err.message);
}
};
Accounts.soundcloud.retrieveCredential = function(credentialToken) {
return Oauth.retrieveCredential(credentialToken);
};
Oauth.registerService('soundcloud', 2, null, handleOauthRequest);