forked from yubozhao/meteor-accounts-instagram
-
Notifications
You must be signed in to change notification settings - Fork 9
/
wechat_server.js
54 lines (44 loc) · 1.48 KB
/
wechat_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
Wechat = {};
OAuth.registerService('wechat', 2, null, function(query) {
var response = getTokenResponse(query);
var accessToken = response.access_token;
var identity = response.user;
var serviceData = _.extend(identity, {accessToken: response.access_token});
return {
serviceData: serviceData,
options: {
profile: { name: identity.full_name },
services: { wechat: identity }
}
};
});
var getTokenResponse = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'wechat'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.post(
"https://api.weixin.qq.com/sns/oauth2/access_token", {
params: {
code: query.code,
appid: config.appId,
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")
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 Wechat. " + err.message),
{response: err.response});
}
return response.content;
};
Wechat.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};