-
Notifications
You must be signed in to change notification settings - Fork 5
/
kakao_server.js
65 lines (59 loc) · 2.19 KB
/
kakao_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
Kakao = {};
OAuth.registerService('kakao', 2, null, function (query) {
var requestAccess = getAccessToken(query);
var identity = JSON.parse(getIdentity(requestAccess));
identity.properties.name = identity.properties.nickname;
return {
serviceData: {
id: identity.id,
accessToken: requestAccess.access_token,
refreshToken: requestAccess.refresh_token,
expires_in: requestAccess.expires_in,
scope: requestAccess.scope
},
options: {profile: identity.properties}
};
});
var getAccessToken = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'kakao'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.post(
"https://kauth.kakao.com/oauth/token", {
params: {
grant_type: 'authorization_code',
client_id: config.clientId,
redirect_uri: Meteor.absoluteUrl() + "_oauth/kakao",
code: query.code
}
});
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with Kakao. " + err.message),
{response: err.response});
}
if (response.data.error) { // if the http response was a json object with an error attribute
throw new Error("Failed to complete OAuth handshake with Kakao. " + response.data.error);
} else {
return response.data;
}
};
var getIdentity = function (requestAccess) {
try {
var authorization = requestAccess.token_type + " " + requestAccess.access_token;
var response = HTTP.post(
"https://kapi.kakao.com/v1/user/me", {
headers: {
Authorization: authorization
}
});
return response.content;
} catch (err) {
throw _.extend(new Error("Failed to fetch identity from Kakao. " + response.content),
{response: err.response});
}
};
Kakao.retrieveCredential = function (credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};