【scrapy】 采用 requests 模拟 Oauth2.0 登录 开源中国社区
【scrapy】 采用 requests 模拟 Oauth2.0 登录 开源中国社区
ok绷forever 发表于6个月前
【scrapy】 采用 requests 模拟 Oauth2.0 登录 开源中国社区
  • 发表于 6个月前
  • 阅读 23
  • 收藏 0
  • 点赞 0
  • 评论 0

本文采用 requests 模拟 oauth2.0 新浪微博接口登录 开源中国社区, 新浪微博的登录 密码采用 AES 进行加密 ,用户名采用 base64 进行编码加密

#! -*- coding:utf-8 -*-
import base64
import urllib
import requests
import re
import rsa
import time
import binascii
import json
from requests.packages import urllib3

def get_username(username):
	username_base64 = base64.b64encode(username)
	return username_base64

def get_password(pass_word,servertime,nonce,spubkey):
	rsaPublickey = int(spubkey, 16)
	key = rsa.PublicKey(rsaPublickey, 65537) #创建公钥
	message = str(servertime) + '\t' + str(nonce) + '\n' + str(pass_word) #拼接明文js加密文件中得到
	passwd = rsa.encrypt(message, key) #加密
	passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。
	return passwd

def gen_url(oriurl,params):
	url = oriurl + '?' + urllib.urlencode(params)
	return url

def get_state(url):
	pattern = "href=\'(.*?)\';</"
	url = re.findall(pattern,url)
	state_loc = url[0].find('state')
	state = url[0][state_loc+6:]
	return state

def get_code(url):
	pattern = "href=\'(.*?)\';</"
	url = re.findall(pattern,url)
	code_loc = url[0].find('code')
	code = url[0][code_loc:code_loc+4]
	return code

headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}

oschina_loginurl = 'https://www.oschina.net/home/login'
ori_params = {
	'goto':'https://www.oschina.net/'
}

login_ori_weibo_url = 'https://www.oschina.net/action/openid/before_bind'
choose_params = {
	'op':'weibo'
}

client_id = 582509177
resonse_type = ''
redirect_uri = 'http://www.oschina.net/action/openid/after_bind'
state = ''
scope = ''

choose_weibo_url = gen_url(login_ori_weibo_url,choose_params)

login_page_url = gen_url(oschina_loginurl,ori_params)
res = requests.Session()
"""
开源中国的登录界面
"""
login_page_content = res.get(login_page_url,headers=headers,verify=False)
# print login_page_content.content
"""
选择微博登录,调用api
"""
choose_weibo_content = res.get(choose_weibo_url,headers=headers,verify=False)
#print choose_weibo_content.content

state = get_state(choose_weibo_content.content)
code = get_code(choose_weibo_content.content)
# print state
"""
微博开始验证  
"""
pattern = "href=\'(.*?)\';</"

url = re.findall(pattern,choose_weibo_content.content)
auth_url = url[0]

after_auth = res.get(auth_url,headers=headers,verify=False)
#print after_auth.content

username = get_username('你的用户名')
login_ori_pre_url = 'https://login.sina.com.cn/sso/prelogin.php'
pre_params = {
	'entry':'openapi',
	'callback':'sinaSSOController.preloginCallBack',
	'su':username,
	'rsakt':'mod',
	'checkpin':'1',
	'client':'ssologin.js(v1.4.18)',
	'_':int(time.time()*1000)
}
login_pre_url = gen_url(login_ori_pre_url,pre_params)
#print login_pre_url
login_pre_result = res.get(login_pre_url,headers=headers,verify=False)
s = (login_pre_result.content)

# p = re.compile('ck(.*)')
# json_data = p.search(s).group(1)
# json_data = json_data[1:-1]

# data = json.loads(json_data)  
# servertime = str(data['servertime'])  
# nonce = data['nonce']  
# pubkey = data['pubkey']  
# rsakv = data['rsakv'] 
# print pubkey

pattern_retcode = 'retcode":(.*?),"'
pattern_servertime = 'servertime":(.*?),"'
pattern_pcid = 'pcid":(.*?),"'
pattern_nonce = 'nonce":(.*?),"'
pattern_pubkey = 'pubkey":(.*?),"'
pattern_rsakv = 'rsakv":(.*?),"'
pattern_is_openlock = 'is_openlock":(.*?),"'
pattern_lm = 'lm":(.*?),"'
pattern_smsurl = 'smsurl":(.*?),"'
pattern_showpin = 'showpin":(.*?),"'
pattern_exectime = 'exectime":(.*?),"'

retcode = re.findall(pattern_retcode,s)[0][1:-1]
servertime = re.findall(pattern_servertime,s)[0]
pcid = re.findall(pattern_pcid,s)[0][1:-1]
nonce = re.findall(pattern_nonce,s)[0][1:-1]
pubkey = re.findall(pattern_pubkey,s)[0][1:-1]
rsakv = re.findall(pattern_rsakv,s)[0][1:-1]
is_openlock = re.findall(pattern_is_openlock,s)[0][1:-1]
lm = re.findall(pattern_lm,s)[0][1:-1]
smsurl = re.findall(pattern_smsurl,s)[0][1:-1]
showpin = re.findall(pattern_showpin,s)[0][1:-1]
# exectime = re.findall(pattern_exectime,s)[0]
password = '你的密码'

password = get_password(password,str(servertime),str(nonce),str(pubkey))

post_formdata = {
	'entry':'openapi',
	'gateway':'1',
	'from':'',
	'savestate':'0',
	'userticket':'1',
	'pagerefer':'https://www.oschina.net/action/openid/before_bind?op=weibo',
	'ct':'1800',
	's':'1',
	'vsnf':'1',
	'vsnval':'',
	'door':'',
	'appkey':'WawKB',
	'su':username,
	'service':'miniblog',
	'servertime':servertime,
	'nonce':nonce,
	'pwencode':'rsa2',
	'rsakv':'1330428213',
	'sp':password,
	'sr':'1366*768',
	'encoding':'UTF-8',
	'cdult':'2',
	'domain':'weibo.com',
	'prelt':'206',
	'returntype':'TEXT'
}

login_weibo_url = 'https://login.sina.com.cn/sso/login.php'
login_weibo_url_params = {
	'client':'ssologin.js(v1.4.18)',
	'_':int(time.time()*1000),
	'openapilogin':'qrcode'
}
login_weibo_url = gen_url(login_weibo_url,login_weibo_url_params)
#print login_weibo_url

after_auth_login_content = res.post(login_weibo_url,data=post_formdata)
# cookies =  requests.utils.dict_from_cookiejar(after_auth_login_content.cookies)



pattern_ticket = 'ticket":"(.*?)","'
ticket = re.findall(pattern_ticket,after_auth_login_content.content)[0]
# print ticket
last_auth_url = 'https://api.weibo.com/oauth2/authorize'

last_formdata = {
	'action':'login',
	'display':'default',
	'withOfficalFlag':'0',
	'quick_auth':'false',
	'withOfficalAccount':'',
	'scope':'',
	'ticket':ticket,
	'isLoginSina':'',
	'response_type':'code',
	'regCallback':'https%3A%2F%2Fapi.weibo.com%2F2%2Foauth2%2Fauthorize%3Fclient_id%3D582509177%26response_type%3Dcode%26display%3Ddefault%26redirect_uri%3Dhttp%253A%252F%252Fwww.oschina.net%252Faction%252Fopenid%252Fafter_bind%253Fgoto%253D%26from%3D%26with_cookie%3D',
	'redirect_uri':'http://www.oschina.net/action/openid/after_bind?goto=',
	'client_id':'582509177',
	'appkey62':'WawKB',
	'state':state,
	'verifyToken':'null',
	'from':'',
	'switchLogin':'0',
	'userId':'',
	'passwd':''
}


refer_url = 'https://api.weibo.com/oauth2/authorize?client_id=582509177&response_type=code&redirect_uri=http%3A%2F%2Fwww.oschina.net%2Faction%2Fopenid%2Fafter_bind%3Fgoto%3D&scope=&state='+state
print refer_url
myheaders = {
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0",
        "Referer":refer_url,
        "Connection":"keep-alive"
    }
success = res.post(last_auth_url,last_formdata,headers=myheaders)
print success.cookies
print success.url
#print success.request.headers['Cookie']
# print cookies
# print success.cookies

home_page = res.get('https://www.oschina.net/news/85180/open-source-software-will-drive-the-future-of-auto-innovations',headers=headers,verify=False)
#print home_page.content
# print home_page.headers
new_headers = home_page.request.headers
# print new_headers
cookies =  home_page.request.headers['Cookie']
# print cookies
new_cookies = {}
pattern_user_behavior = 'ior_=(.*?); '
_user_behavior = re.findall(pattern_user_behavior,cookies)[0]
oscid_loc = cookies.find('oscid')
new_cookies['_user_behavior'] = _user_behavior
new_cookies['oscid'] = cookies[oscid_loc+6:]
print new_cookies
headers['Cookie'] = cookies
#print headers
new_res = requests.Session()
cookies = requests.utils.cookiejar_from_dict(new_cookies, cookiejar=None, overwrite=True)
test_page = new_res.get('https://www.oschina.net/',headers=headers,verify=False)
#print test_page.request.headers
f = open(u'E:\中国开源社区.txt','wb')
f.write(test_page.content)
f.close()
# print success.content
# print success.headers
# print success.cookies
# print success.url
共有 人打赏支持
粉丝 4
博文 114
码字总数 53609
×
ok绷forever
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: