1 2 3 4 5 6 7 8 9 10 11 | class LoginAuth(): consumer_key='your key' consumer_secret='your secret' def basicAuth(self, source, username, password): try: self.authType = 'basicauth' self.auth = BasicAuthHandler(username, password) self.api = API(self.auth,source=source) except Exception as return '' |
1 2 3 4 5 6 7 8 9 | def verify_credentials(self): try: return bind_api( path = '/account/verify_credentials.json', payload_type = 'user', require_auth = True )(self) except WeibopError: return False |
1 2 3 4 5 6 7 8 9 10 11 12 | @pyqtSlot()# 定义消息处理函数 def on_loginBtn_clicked(self): la=LoginAuth() la.basicAuth('536097958', self.usernameText.text(), self.pwdText.text()) if la.api.verify_credentials() == False: self.loginResultLabel.setText("登录失败") else: self.loginResultLabel.setText("登录成功") sendBox = SendMsgUI() sendBox.show() sendBox.setUserInfo(self.usernameText.text(), self.pwdText.text()) sendBox.exec() |
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 | def token(user,password): """根据用户名和密码产生 token""" newUser = '%s:%s'%(user,password) return base64.b64encode(newUser.encode()).strip() def update(token,message): """发送微博消息""" conn=http.client.HTTPConnection('api.t.sina.com.cn') header={} header.setdefault('Authorization',"Basic %s" % token) params=urllib.parse.urlencode({'status':message,'source':'536097958'}) print(params) count = 0 while True : count = count + 1 conn.request('POST', '/statuses/update.json?%s'%params, headers=header) resp = conn.getresponse() if resp.status == 200 : print('发送成功') return 1 else : if count > 3 : print('发送失败次数过多 , 3166 !') return -1 else : print(resp.read()) print('发送失败,一秒钟再试 !') sleep(1) conn.close() |
1 2 3 4 5 6 7 8 9 | @pyqtSlot() def on_pushButton_clicked(self): """ Slot documentation goes here. """ # TODO: not implemented yet msg = self.msgEdit.toPlainText() update(token(self.uName,self.pwd).decode(),msg) self.exec() |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |