forked from awesomepan/meteor-accounts-qq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qq_server.js
executable file
·116 lines (102 loc) · 3.43 KB
/
qq_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
114
115
116
Qq = {};
OAuth.registerService('qq', 2, null, function(query) {
var response = getTokenResponse(query);
return {
serviceData: {
id: response.openId,
accessToken: response.accessToken,
nickname: response.userInfo.nickname
},
options: {
profile: { name: response.userInfo.nickname },
}
};
});
var getTokenResponse = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'qq'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.get(
"https://graph.qq.com/oauth2.0/token", {
params: {
code: query.code,
client_id: config.clientId,
redirect_uri: OAuth._redirectUri("qq", config),
client_secret: OAuth.openSecret(config.secret),
grant_type: 'authorization_code'
}
});
if (response.error) // if the http response was an error
throw response.error;
if (typeof response.content === "string")
var qqAccessToken;
_.each(response.content.split('&'), function (kvString) {
var kvArray = kvString.split('=');
if (kvArray[0] === 'access_token')
qqAccessToken = kvArray[1];
});
if (response.content.error)
throw response.content;
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with QQ. " + err.message),
{response: err.response});
}
try {
response = HTTP.get(
"https://graph.qq.com/oauth2.0/me", {
params: {
access_token: qqAccessToken
}
});
if (response.error) // if the http response was an error
throw response.error;
if (typeof response.content === "string")
// The response content in /me requires trickly JSONP callback to parse
var meContent = {};
var callbackExp = /^\s*callback\s*\((.+)\)\s*;\s*$/;
var matched = response.content.match(callbackExp);
if (matched && matched.length === 2) {
meContent = JSON.parse(matched[1]);
if (meContent.error) {
console.log("Error in getting account's open id, details: " + meContent.error);
throw new Error(meContent.error);
}
} else {
throw new Error("Error in getting account's open id");
}
if (response.content.error)
throw response.content;
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with QQ. " + err.message),
{response: err.response});
}
try {
response = HTTP.get(
"https://graph.qq.com/user/get_user_info", {
params: {
access_token: qqAccessToken,
oauth_consumer_key: config.clientId,
openid: meContent.openid
}
});
if (response.error) // if the http response was an error
throw response.error;
if (typeof response.content === "string")
var userInfoContent = JSON.parse(response.content);
if (response.content.error)
throw response.content;
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with QQ. " + err.message),
{response: err.response});
}
return {
accessToken: qqAccessToken,
openId: meContent.openid,
userInfo: userInfoContent
}
};
Qq.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};