def authorize_clent(client, flags): logging.getLogger().setLevel(getattr(logging, flags.logging_level)) user_data_dir = os.sep.join([os.getenv("HOME"), '.weibo4pic']) if not os.path.isdir(user_data_dir): try: os.mkdir(user_data_dir) except: raise Exception('OSError') client_access_token_file = os.sep.join([user_data_dir, 'client_access_token.json']) try: f = open(client_access_token_file, 'r') data = f.readline() f.close() obj_data = _parse_json(data) client = _authorize_clent(client, obj_data) if client.expires > time.time(): return client else: pass except: pass if not flags.noauth_local_webserver: success = False port_number = flags.auth_host_port try: httpd = ClientRedirectServer((flags.auth_host_name, port_number), ClientRedirectHandler) except socket.error, e: pass else: success = True flags.noauth_local_webserver = not success if not success: print 'Failed to start a local webserver listening on either port 8090' print 'Please check your firewall settings and locally' print 'running programs that may be blocking or using those ports.' print print 'Falling back to --noauth_local_webserver and continuing with', print 'authorization.' print
def Parse_get_text_from_json(self, texts, text='text'): ''' get text content from a json string ''' json_texts = weibo._parse_json(texts) return json_texts.get(text, False)
def get_code(self): # 预登陆 prelogin_url = ( 'http://login.sina.com.cn/sso/prelogin.php?' 'entry=openapi&callback=sinaSSOController.preloginCallBack&su=%s&' 'rsakt=mod&checkpin=1&client=ssologin.js(v1.4.15)&_=1400822309846' ) % self.username prelogin_req = urllib.request.Request(prelogin_url) prelogin_res = urllib.request.urlopen(prelogin_req) prelogin_res_text = prelogin_res.read().decode('utf-8') # 获取预登陆得到的参数 servertime = re.findall('"servertime":(.*?),', prelogin_res_text)[0] pubkey = re.findall('"pubkey":"(.*?)",', prelogin_res_text)[0] rsakv = re.findall('"rsakv":"(.*?)",', prelogin_res_text)[0] nonce = re.findall('"nonce":"(.*?)",', prelogin_res_text)[0] # 数据加密 su = base64.b64encode( bytes(urllib.request.quote(self.username), encoding='utf-8')) pubkey = int(pubkey, 16) key = rsa.PublicKey(pubkey, 65537) message = bytes(str(servertime) + '\t' + str(nonce) + '\n' + str(self.password), encoding='utf-8') sp = binascii.b2a_hex(rsa.encrypt(message, key)) # 构建Post参数 login_param = { 'entry': 'openapi', 'gateway': 1, 'from': '', 'savestate': 0, 'useticket': 1, 'pagerefer': '', 'ct': 1800, 's': 1, 'vsnf': 1, 'vsnval': '', 'door': '', 'su': su, 'cdult': 2, 'returntype': 'TEXT', 'service': 'miniblog', 'servertime': servertime, 'nonce': nonce, 'pwencode': 'rsa2', 'rsakv': rsakv, 'sp': sp, 'sr': '1680*1050', 'encoding': 'UTF-8', 'prelt': 961 } login_param = urllib.parse.urlencode(login_param).encode('utf-8') # 登录 login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)' login_headers = { 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)' } login_req = urllib.request.Request(login_url, login_param, login_headers) login_res = urllib.request.urlopen(login_req) login_res_text = login_res.read().decode('gbk') login_res_json = _parse_json(login_res_text) if not login_res_json.retcode == '0': raise WeiboLoginError(login_res_json.retcode, login_res_json.reason) else: # 认证,获取code auth_url = "https://api.weibo.com/oauth2/authorize" auth_headers = { 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)', 'Referer': 'https://api.weibo.com/oauth2/authorize' } auth_param = { 'client_id': self.client_id, 'redirect_uri': self.redirect_url, 'response_type': 'code', 'ticket': login_res_json.ticket, 'action': 'login' } auth_param = urllib.parse.urlencode(auth_param).encode('utf-8') auth_req = urllib.request.Request(auth_url, auth_param, auth_headers) auth_res = urllib.request.urlopen(auth_req) code = re.findall('code=(.*)', auth_res.url)[0] return code
def parseMsg(self,msg): return _parse_json(msg)