-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount.js
84 lines (78 loc) · 2.71 KB
/
account.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
var qs = require('querystring');
var settings = require('./etc/settings.json');
var OAuth = require('oauth').OAuth;
var db = require('./lib/db').db;
db.init(settings);
var uri1 = 'http://api.t.sina.com.cn/oauth/request_token';
var uri2 = 'http://api.t.sina.com.cn/oauth/access_token';
var uri3 = 'http://api.t.sina.com.cn/oauth/authorize';
var authorize = function(account, token){
var params = {
oauth_token:token,
oauth_callback:'json',
display:'json',
userId: account.email,
passwd: account.password
};
}
var oauth = new OAuth(uri1, uri2, settings.weibo.appkey, settings.weibo.secret, '1.0', '', 'HMAC-SHA1');
var getAccessToken = function(account, cb){
oauth.getOAuthRequestToken(function(err, oauthToken, oauthTokenSecret, result){
var params = {
oauth_token:oauthToken,
oauth_callback:'json',
display:'json',
userId: account.email,
passwd: account.password
};
var url = uri3 + '?' + qs.stringify(params);
oauth.get(url, settings.weibo.appkey, settings.weibo.secret, function(error, body){
if(error){
cb(error , null);
}else{
body = JSON.parse(body);
oauth.getOAuthAccessToken(oauthToken, oauthTokenSecret, body.oauth_verifier, function(error, accessToken, accessTokenSecret, results){
if(error){
cb(error, null);
return;
}
account.access_token = accessToken;
account.access_token_secret = accessTokenSecret;
account.weibo_user_id = results.user_id;
cb(error, account);
});
}
});
});
};
db.loadAccounts(function(error, accounts){
if(error){
console.log(['load accounts error:', error]);
return;
}
var was = [];
for(var code in accounts){
was.push(accounts[code]);
}
var update = function(error, account){
if(error){
console.log(['request token error:', error, account]);
}else{
db.updateAccount(account, function(error, info){
if(error){
console.log(['update error:', account, error]);
}else{
console.log(['update success:', account]);
}
});
if(was.length > 0){
console.log('rest ' + was.length);
setTimeout(function(){
getAccessToken(was.pop(), update);
}, 100);
}
}
}
var account = was.pop();
getAccessToken(account, update);
});