-
Notifications
You must be signed in to change notification settings - Fork 0
/
weibologin2.py
137 lines (120 loc) · 3.85 KB
/
weibologin2.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import re
import json
from urlparse import urlparse, parse_qs
import requests
import traceback
from requests.compat import cookielib
import time
APP_KEY = "xxxxx"
APP_SECRET = 'xxxxx'
CALLBACK_URL = 'http://2.xweiboproxy.sinaapp.com/callback.php'
RSA_SERVER_URL = 'http://localhost:8888/encrypt?password=%s'
session = requests.session()
session.timeout = 10
postdata = {
'entry': 'weibo',
'gateway': '1',
'from': '',
'savestate': '7',
'userticket': '1',
'ssosimplelogin': '1',
'vsnf': '1',
'su': '',
'service': 'miniblog',
'servertime': '',
'nonce': '',
'pwencode': 'rsa2',
'rsakv': '',
'sp': '',
'encoding': 'UTF-8',
'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
'returntype': 'META'
}
def __get_servertime():
'''
获取服务器时间和nonce随机数
'''
url = 'http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinaSSOController.preloginCallBack&su=dW5kZWZpbmVk&rsakt=mod&client=ssologin.js(v1.4.1)&_=1344575024059'
data = requests.get(url).text;
p = re.compile('\((.*)\)')
try:
json_data = p.search(data).group(1)
data = json.loads(json_data)
servertime = str(data['servertime'])
nonce = data['nonce']
rsakv = data['rsakv']
return servertime, nonce, rsakv
except:
print 'Get severtime error!'
return None
def __get_pwd(pwd):
'''
RSA加密
'''
global session
resp = session.get(RSA_SERVER_URL % pwd)
return resp.content
def __get_user(username):
'''
username 经过了BASE64 计算
'''
username_ = requests.compat.quote(username)
username = base64.encodestring(username_)[:-1]
return username
def login(username, pwd):
global session
url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.2)'
try:
servertime, nonce, rsakv = __get_servertime()
except Exception, e:
print e
return None
global postdata
postdata['servertime'] = servertime
postdata['nonce'] = nonce
postdata['su'] = __get_user(username)
postdata['sp'] = __get_pwd(pwd)
postdata['rsakv'] = rsakv
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.168 Chrome/18.0.1025.168 Safari/535.19'}
result = session.post(
url=url,
data=postdata,
headers=headers,
)
text = result.text
p = re.compile('location\.replace\(\"(.*?)\"\)')
try:
COOKIEJAR_CLASS = cookielib.LWPCookieJar
cookiejar = COOKIEJAR_CLASS('weibo_cookies')
login_url = p.search(text).group(1)
session.get(login_url, cookies=cookiejar)
cookiejar.save()
print 'Login Success!'
except Exception:
print traceback.format_exc()
return None
def getToken():
authorize_url = "https://api.weibo.com/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=token" % (
APP_KEY, CALLBACK_URL)
response = session.get(authorize_url, allow_redirects=False)
callback_url = response.headers.get('location')
qs = parse_qs(urlparse(callback_url).fragment)
token = qs.get('access_token')
expires_in = qs.get('expires_in')
if token:
# expires等于当前时间加上有效期,单位为秒
expires = int(expires_in[0]) + int(time.time())
return token[0], expires
class WeiboError(StandardError):
def __init__(self, error_code, error):
self.error_code = error_code
self.error = error
StandardError.__init__(self, error)
def __str__(self):
return 'TokenGeneratorError: ErrorCode: %s, ErrorContent: %s' % (self.error_code, self.error)
if __name__ == '__main__':
login('xxxxx', 'xxxxx')