Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (c) 2013-2015 Sergei Snegirev <tz4678@gmail.com>
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
- from vkapi import *
- from vkapi.auth import CLIENT_ID, CLIENT_SECRET, SCOPE, DirectAuth, AuthError
- from PyQt5.QtWidgets import QDialog, QLineEdit, QMessageBox
- from PyQt5.QtGui import QPixmap
- from .ui_CaptchaDialog import Ui_CaptchaDialog
- from .ui_AuthDialog import Ui_AuthDialog
- __author__ = "Sergei Snegirev <tz4678@gmail.com>"
- __license__ = "MIT"
- class CaptchaDialog(QDialog):
- def __init__(self, client, image_url, parent=None):
- super().__init__(parent)
- self.client = client
- self.image_url = image_url
- self.ui = Ui_CaptchaDialog()
- self.ui.setupUi(self)
- self.ui.refreshButton.clicked.connect(self.refresh)
- self.loadImage()
- def loadImage(self):
- data = self.client.get(self.image_url).content
- pixmap = QPixmap()
- pixmap.loadFromData(data)
- self.ui.captchaLabel.setPixmap(pixmap)
- def refresh(self):
- self.loadImage()
- self.ui.captchaEdit.setText("")
- self.ui.captchaEdit.setFocus()
- # Данные от официального приложения для iPhone
- CLIENT_ID = 3140623
- CLIENT_SECRET = 'VeWdmVclDCtn6ihuP1nt'
- SCOPE = 'notify,friends,photos,audio,video,docs,notes,pages,status,offers,' + \
- 'questions,wall,groups,messages,email,notifications,stats,ads,offline,nohttps'
- class DirectAuthDialog(QDialog):
- def __init__(self,
- client,
- client_id=CLIENT_ID,
- client_secret=CLIENT_SECRET,
- scope=SCOPE,
- parent=None):
- super().__init__(parent)
- self.client = client
- self.client_id = client_id
- self.client_secret = client_secret
- self.scope = scope
- self.ui = Ui_AuthDialog()
- self.ui.setupUi(self)
- self.ui.loginButton.clicked.connect(self.login)
- self.ui.showPasswordCheck.stateChanged.connect(self.showPassword)
- def showPassword(self, state):
- mode = QLineEdit.Normal if state else QLineEdit.Password
- self.ui.passwordEdit.setEchoMode(mode)
- def login(self):
- username = self.ui.usernameEdit.text()
- password = self.ui.passwordEdit.text()
- error = ""
- if not username:
- error = "Username field is required"
- elif not password:
- error = "Password field is required"
- else:
- self.ui.loginButton.setDisabled(True)
- params = dict(username=username,
- password=password,
- client_id=self.client_id,
- client_secret=self.client_secret,
- scope=self.scope,
- grant_type="password")
- try:
- self._login(params)
- except AuthError as exc:
- error = str(exc)
- self.ui.loginButton.setEnabled(True)
- if error:
- QMessageBox.warning(self, "Authorization Error", error)
- else:
- QDialog.accept(self)
- def _login(self, params):
- url = 'https://oauth.vk.com/token'
- r = self.client.post(url, params)
- data = r.json()
- if 'error' in data:
- error = AuthError(data)
- if error.reason == 'need_captcha':
- d = CaptchaDialog(self.client, error.captcha_img, self)
- if d.exec_():
- params['captcha_key'] = d.ui.captchaEdit.text()
- params['captcha_sid'] = error.captcha_sid
- return self._login(params)
- raise error
- self.client.access_token = data['access_token']
- self.client.token_expires = data['expires_in']
- if self.client.token_expires > 0:
- self.client.token_expires += time.time()
- self.client.user_id = data.get('user_id')
- self.client.secret_token = data.get('secret')
- # vkapi/auth.py
- ################################################################################
- # Copyright (c) 2013-2015 Sergei Snegirev <tz4678@gmail.com>
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
- import time
- __author__ = "Sergei Snegirev <tz4678@gmail.com>"
- __license__ = "MIT"
- # Данные от официального приложения для iPhone
- CLIENT_ID = 3140623
- CLIENT_SECRET = 'VeWdmVclDCtn6ihuP1nt'
- SCOPE = 'notify,friends,photos,audio,video,docs,notes,pages,status,offers,' + \
- 'questions,wall,groups,messages,email,notifications,stats,ads,offline,nohttps'
- class DirectAuth:
- AUTH_URL = 'https://oauth.vk.com/token'
- def __init__(self,
- client,
- client_id=CLIENT_ID,
- client_secret=CLIENT_SECRET,
- scope=SCOPE):
- self.client = client
- self.client_id = client_id
- self.client_secret = client_secret
- self.scope = scope
- def auth(self, username, password):
- params = dict(username=username,
- password=password,
- client_id=self.client_id,
- client_secret=self.client_secret,
- scope=self.scope,
- grant_type="password")
- def try_auth():
- data = self.client.post(self.AUTH_URL, params).json()
- if 'error' in data:
- error = AuthError(data)
- if error.reason == 'need_captcha':
- params['captcha_key'] = self.solveCaptcha(error.captcha_img)
- params['captcha_sid'] = error.captcha_sid
- return try_auth()
- raise error
- self.client.access_token = data['access_token']
- self.client.token_expires = data['expires_in']
- if self.client.token_expires > 0:
- self.client.token_expires += time.time()
- self.client.user_id = data.get('user_id')
- self.client.secret_token = data.get('secret')
- try_auth()
- def solveCaptcha(self, image_url):
- raise NotImplementedError("This method is not implemented yet")
- class AuthError(Exception):
- def __init__(self, details):
- super().__init__("{error}: {error_description}".format(**details) \
- if 'error_description' in details else details['error'])
- self.reason = details['error']
- self.description = details.get('error_description')
- self.captcha_img = details.get('captcha_img')
- self.captcha_sid = details.get('captcha_sid')
- self.redirect_uri = details.get('redirect_uri')
- ################################################################################
- class UiClient(Client):
- def __init__(self, widget=None, **kw):
- super().__init__(**kw)
- self.widget = widget
- def solveCaptcha(self, image_url):
- d = CaptchaDialog(self, image_url, self.widget)
- if d.exec_():
- return d.ui.captchaEdit.text()
- raise ClientError("Request canceled")
- def
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement