写下这个标题的时候,您可能要问腾讯产品登录协议有什么用?很简单,qq所有产品都是采用统一的登录协议加密登录的。也就是说,当您能够通过使用它的协议来登录并获取到令牌后,那么就可以任意的使用qq的各项产品了,如webqq,空间,微博,校友。。等等的。而本文主要就是讲述下如何通过技术手段来使用腾讯产品登录协议,从而获取到登录令牌。所有完整的源代码,可以从我的开源项目 CDM SYSTEM中的pushblog插件获取并且试用。地址是:http://code.google.com/p/linblog/
1
正常时,请求这个地址,会获得类似ptui_checkVC(
'0'
,
'!AFO'
);这样的返回值,这其中0是代表正常状态,不采用图片验证。但是如果短时间内这个帐号多次在不同的ip登录,那么返回值就是另外的一种返回值了,那表明需要进行图片验证码验证。
01
def
Get_qq_msg_val(
self
,username,password):
02
verifyURL
=
'http://ptlogin2.qq.com/check?uin=%s&appid=46000101&r=%s'
%
(username,random.random())
03
loginURL
=
'http://ptlogin2.qq.com/login?'
04
redirectURL
=
''
05
cookie
=
''
06
qqn
=
username
07
md5Pass
=
''
08
verifyCode
=
''
09
result
=
urlfetch.fetch(url
=
verifyURL,method
=
urlfetch.GET,
10
follow_redirects
=
False
,headers
=
{
11
'Content-Type'
:
'application/x-www-form-urlencoded'
,
12
'user-agent'
:
'Mozilla/5.0 (Linux; U; Linux i686; en-US) AppleWebKit/525.13 (KHTML, like Gecko)_chrome/0.4.2.80 Safari/525.13'
,
13
},)
14
cookie1
=
Cookie.SimpleCookie(result.headers.get(
'set-cookie'
, ''))
15
verifyCode
=
result.content[
18
:
-
3
]
16
if
len
(verifyCode)>
4
:
17
return
False
,
None
18
loginURL
+
=
"u=%s&p="
%
username
19
loginURL
+
=
self
.EncodePasswordWithVerifyCode(password,verifyCode)
20
loginURL
+
=
"&verifycode="
+
verifyCode
+
"&aid=46000101&u1=http%3A%2F%2Ft.qq.com&ptredirect=1&h=1&from_ui=1&fp=loginerroralert"
21
result
=
urlfetch.fetch(url
=
loginURL,
22
headers
=
{
'Referer'
:
'http://t.qq.com'
,
23
'Cookie'
:
self
.make_cookie_header(cookie1),
24
'Content-Type'
:
'application/x-www-form-urlencoded'
,
25
'user-agent'
:
'Mozilla/5.0 (Linux; U; Linux i686; en-US) AppleWebKit/525.13 (KHTML, like Gecko)_chrome/0.4.2.80 Safari/525.13'
,
26
},
27
method
=
urlfetch.GET,
28
follow_redirects
=
False
,
29
)
30
setCookies
=
result.headers.get(
'set-cookie'
, '
').split('
;')
31
cookie2
=
''
32
cookie2
+
=
setCookies[
29
]
33
cookie2
+
=
setCookies[
7
]
34
cookie2
+
=
setCookies[
4
]
35
cookie2
+
=
';'
+
setCookies[
0
]
36
cookie2
=
cookie2.replace(
','
,
';'
)
37
cookie2
=
cookie2[
1
:]
38
callback_url
=
result.headers.get(
'location'
,
'http://t.qq.com'
)
39
result,cookies
=
self
.do_redirect(callback_url, cookie2)
40
41
return
result,cookies
1
def
md5hash(
self
,
str
):
2
return
hashlib.md5(
str
).digest()
3
def
hex_md5hash(
self
,
str
):
4
return
hashlib.md5(
str
).hexdigest().upper()
5
def
md5hash_3(
self
,
str
):
6
return
self
.hex_md5hash(
self
.md5hash(
self
.md5hash(
str
)))
7
def
EncodePasswordWithVerifyCode(
self
,pwd, verifyCode):
8
return
self
.hex_md5hash(
self
.md5hash_3(pwd)
+
verifyCode.upper())
1
EncodePasswordWithVerifyCode()即为此加密算法,pwd是密码,verifyCode为我们第一步获取到的
4
位的原始密钥。
01
pt2gguin o0939567050 / qq.com Fri, 02-Jan-2020 00:00:00 GMT
02
uin o0939567050 / qq.com End Of Session
03
skey @aw6EH0ljo / qq.com End Of Session
04
clientuin / qq.com Fri, 02-Jan-1970 00:00:00 GMT
05
clientkey / qq.com Fri, 02-Jan-1970 00:00:00 GMT
06
zzpaneluin / qq.com Fri, 02-Jan-1970 00:00:00 GMT
07
zzpanelkey / qq.com Fri, 02-Jan-1970 00:00:00 GMT
08
ptisp ctc / qq.com End Of Session
09
ptcz f41721c4e7bc752fdeae10c9cb2db66ca66af1049a4835185acfdfe2a26860e5 / qq.com Fri, 02-Jan-2020 00:00:00 GMT
10
airkey / qq.com Fri, 02-Jan-1970 00:00:00 GMT
01
def
do_redirect(
self
,url, cookie):
02
logging.info(url)
03
result
=
urlfetch.fetch(
04
url
=
url,
05
headers
=
{
'Cookie'
:cookie,
06
'Content-Type'
:
'application/x-www-form-urlencoded'
,
07
'user-agent'
:
'Mozilla/5.0 (Linux; U; Linux i686; en-US) AppleWebKit/525.13 (KHTML, like Gecko)_chrome/0.4.2.80 Safari/525.13'
,},
08
method
=
urlfetch.GET,
09
follow_redirects
=
False
,
10
)
11
return
result,cookie
01
def
send_qq_msgs(
self
,username,password,msg,memcachekey):
02
"""
03
send qq msgs. use qq username, password.
04
the msgs parameter is a message list, not a single string.
05
"""
06
cookie
=
''
07
if
memcache.get(memcachekey):
08
cookie
=
memcache.get(memcachekey)
09
else
:
10
result,oldcookie
=
self
.Get_qq_msg_val(username,password)
11
if
result
=
=
False
:
12
return
False
13
cookie
=
'%s;%s'
%
(result.headers.get(
'set-cookie'
, ''),oldcookie)
14
memcache.
set
(memcachekey,cookie,
36000
)
15
msg
=
unescape(msg)
01
form_fields
=
{
02
"content"
: msg,
03
"pic"
:'',
04
"countType"
:'',
05
"viewModel"
:
1
06
}
07
form_data
=
urllib.urlencode(form_fields)
08
try
:
09
result
=
urlfetch.fetch(url
=
"http://t.qq.com/publish.php?rnd=0.5100760071072727"
,
10
payload
=
form_data,
11
method
=
urlfetch.POST,
12
headers
=
{
'Referer'
:
'http://t.qq.com'
,
13
'Cookie'
: cookie,
14
'user-agent'
:
'Mozilla/5.0 (Linux; U; Linux i686; en-US) AppleWebKit/525.13 (KHTML, like Gecko)_chrome/0.4.2.80 Safari/525.13'
,
15
16
},follow_redirects
=
False
)
17
except
:
18
return
False
19
if
result.status_code
=
=
200
:
20
return
True
21
else
:
22
return
False
01
def
send_qzone2(
self
,username,password,content,title,html):
02
"""
03
send qzone blog. use username, password,content,title,html.
04
the content parameter like html,but no html tag.
05
"""
06
memcachekey
=
'send_qzone2'
07
cookie
=
''
08
if
memcache.get(memcachekey):
09
cookie
=
memcache.get(memcachekey)
10
logging.info(
'get cookie from memcache'
)
11
else
:
12
result,oldcookie
=
self
.Get_qzone2_val(username,password)
13
cookie
=
'%s;%s'
%
(result.headers.get(
'set-cookie'
, ''),oldcookie)
14
memcache.
set
(memcachekey,cookie,
36000
)
15
logging.info(
'set cookie'
)
16
tmphash
=
self
.Tmp_skey_get(cookie)
17
tmphash
=
self
.myhash(tmphash)
18
category
=
'个人日记'
19
form_fields
=
{
20
"uin"
:
'939567050'
,
21
"category"
:htmllib.encoding(category,
'gb18030'
),
22
"title"
:title,
23
"content"
:content,
24
"html"
:html,
25
"cb_autograph"
:
'1'
,
26
"topflag"
:
'0'
,
27
"needfeed"
:
'0'
,
28
"lp_type"
:
'0'
,
29
"g_tk"
:tmphash,
30
"scorr_20100723_"
:
'http://qzs.qq.com/qzone/newblog/v5/editor.html|http://qzs.qq.com/qzone/newblog/v5/editor.html<http://user.qzone.qq.com/939567050/main'
,
31
}
32
form_data
=
urllib.urlencode(form_fields)
33
try
:
34
result
=
urlfetch.fetch(url
=
"http://b.qzone.qq.com/cgi-bin/blognew/blog_add"
,
35
payload
=
form_data,
36
method
=
urlfetch.POST,
37
headers
=
{
'Referer'
:
'http://imgcache.qq.com/qzone/v5/toolpages/fp_gbk.html'
,
38
'Cookie'
: cookie,
39
'user-agent'
:
'Mozilla/5.0 (Linux; U; Linux i686; en-US) AppleWebKit/525.13 (KHTML, like Gecko)_chrome/0.4.2.80 Safari/525.13'
,
40
41
},follow_redirects
=
False
)
42
except
Exception,data:
43
logging.info(data)
44
return
False
45
if
result.status_code
=
=
200
:
46
return
True
47
else
:
48
return
False