forked from yubozhao/meteor-accounts-instagram
-
Notifications
You must be signed in to change notification settings - Fork 1
/
untappd_server.js
113 lines (93 loc) · 2.97 KB
/
untappd_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Untappd = {};
Oauth.registerService('untappd', 2, null, function (query) {
const config =
ServiceConfiguration.configurations.findOne({ service: 'untappd' });
const response = getTokenResponse(config, query);
const accessToken = response.response.access_token;
const identity = getIdentity(config, accessToken);
const serviceData = _.extend(identity, { accessToken: accessToken });
return {
serviceData: serviceData,
options: {
profile: { name: [identity.first_name, identity.last_name].join(' ') },
services: { untappd: identity }
}
};
});
const getTokenResponse = function (config, query) {
if (!config) {
throw new ServiceConfiguration.ConfigError();
}
var response;
try {
const absoluteUrlOptions = {};
const rootUrl = Untappd.rootUrl();
if (rootUrl) {
absoluteUrlOptions.rootUrl = rootUrl;
}
var uri = OAuth._redirectUri('untappd', config, null, absoluteUrlOptions);
const options = {
params: {
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
response_type: 'code',
redirect_url: uri.replace('?close', ''),
code: query.code
}
};
response = HTTP.get('https://untappd.com/oauth/authorize/', options);
if (response.error) { // if the http response was an error
throw response.error;
}
if (typeof response.content === 'string') {
response.content = JSON.parse(response.content);
}
if (response.content.error) {
throw response.content;
}
} catch (err) {
throw _.extend(
new Error('Failed to complete OAuth handshake with Untappd. ' + err.message),
{response: err.response}
);
}
return response.content;
};
const getIdentity = function (config, accessToken) {
try {
const options = {
params: {
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
access_token: accessToken
}
};
const response = HTTP.get('https://api.untappd.com/v4/user/info/', options);
if (response.statusCode !== 200) {
throw new Error('Could not retrieve identity');
}
if (!response.data || !response.data.meta || response.data.meta.code !== 200) {
throw new Error('Could not retrieve identity');
}
const data = ifNull(response.data.response, {});
return _.extend(ifNull(data.user, {}), {
email: ifNull(data.settings, {}).email_address,
user: data.user_name,
user_id: data.uid
});
} catch (err) {
throw _.extend(
new Error('Failed to fetch identity. ' + err.message),
{ response: err.response }
);
}
};
const ifNull = function (val, def) {
return val || def;
};
Untappd.retrieveCredential = function (credentialToken, credentialSecret) {
return Oauth.retrieveCredential(credentialToken, credentialSecret);
};
// override this method to set the root redirect URL
// useful for multi-tenant environments
Untappd.rootUrl = function () { /* noop */ };