import json import time import requests OAUTH_CLIENT_ID = '4INGOCMoulE0fNY1SQlTbPtsWqqxGj2DdqjADq6u' OAUTH_CLIENT_SECRET = 'KPc4dMSztNwAIB3vii3geXrzC1mKUIsAztz3t2ylC3HlrgJudJWhdrtoY6XeRJIuadTAREYYsXk9XtFHUDfPVcJvyfHMpNkz2VvghwrijhVprok0VYV7XrOirJ5nFUxD' API_URI = 'https://accounts.gooyal.ir' # API_URI = 'http://127.0.0.1:8000' class ApiClient(): def __init__(self, phone_number, auth_data=None): self.auth_data = {} if auth_data: self.auth_data = auth_data self.phone_number = phone_number def otp_login(self, password): phone_number = self.phone_number data = { "grant_type": "password", "username": phone_number, "password": password, "scope": 'introspection', "auth_fields": 'phone_number:otp' } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, auth=auth, verify=False ) auth_data = response.json() if 'access_token' in auth_data: self.auth_data = auth_data print(auth_data) return auth_data def ott_login(self, token): phone_number = self.phone_number data = { "grant_type": "password", "username": phone_number, "password": token, # "scope": 'introspection education.course:retrieve education.course:submit superapp.applications:list accounts.account:request_ott accounts.profile:list accounts.profile:retrieve accounts.account:retrieve accounts.account:update accounts.profile:inquiry accounts.account:change_password wallet.transaction:list wallet.invoice:create wallet.transaction:retrieve wallet.invoice:pay wallet.invoice:receipt wallet.deposit:submit wallet.deposit:verify wallet.withdraw:submit wallet.withdraw:verify ', "scope": 'introspection', "auth_fields": 'phone_number:ott' } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, auth=auth, verify=False ) auth_data = response.json() if 'access_token' in auth_data: self.auth_data = auth_data return auth_data def login_as_client_credentials(self): access_token = self.auth_data.get('access_token') expires_in = self.auth_data.get('expires_in') created_at = self.auth_data.get('created_at') if access_token and expires_in > time.time() - created_at: return self.auth_data data = { "grant_type": 'client_credentials', "scope": 'wallet.deposit:submit wallet.deposit:verify', } auth = (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET) response = requests.post(f'{API_URI}/oauth2/token/', data=data, auth=auth) auth_data = response.json() print(auth_data) if 'access_token' in auth_data: auth_data['created_at'] = time.time() self.auth_data = auth_data return auth_data def _request(self, url, data=None, files=None, method='get', with_auth=True, encode=True): if files: header = {} else: header = { "Content-Type": "application/json", "charset": "utf-8" } if with_auth: access_token = self.auth_data.get('access_token', '') header.setdefault("Authorization", f"Bearer {access_token}", ) if data and encode: data = json.dumps(data) response = requests.request(method, url, headers=header, data=data, files=files, verify=False) try: return response.json() except: return response.content.decode() def request_otp(self): phone_number = self.phone_number path = 'users/api/request_otp/' url = f'{API_URI}/{path}' method = 'post' data = { 'phone_number': phone_number } result = self._request(url=url, data=data, method=method, with_auth=False) return result def get_application_list(self, page=None): path = 'api/applications/' if page: page = int(page) path = f'{path}?page={page}' url = f'{API_URI}/{path}' return self._request(url=url) def get_course_list(self): url = 'https://edu-srv.mindplus-dev.ir/api/courses/' return self._request(url=url) def get_application_token(self): path = 'users/api/request_ott/' data = { 'application': 'https://edu.mindplus-dev.ir' } url = f'{API_URI}/{path}' response = self._request(url, method='POST', data=data) return response and 'code' in response, response def update_account(self, first_name): path = '/users/api/account/' data = { 'first_name': first_name } url = f'{API_URI}/{path}' response = self._request(url, method='PUT', data=data) print(response) return response and 'code' in response, response def introspect_account(self, token): path = '/oauth2/introspect/' data = { 'token': token } headers = { 'Content-Type': 'application/x-www-form-urlencoded' } access_token = self.auth_data.get('access_token', '') headers.setdefault("Authorization", f"Bearer {access_token}", ) response = requests.post( url=f'{API_URI}/{path}', headers=headers, verify=False, data=data) return response.json() def get_wallet_balance(self): url = 'http://127.0.0.1:8000/wallet/api/wallet/default/balance' return self._request(url=url) def get_application_preferences(self): url = 'https://preferences.gooyal.com/data/application/' return self._request(url=url) auth_data = {'access_token': 'DJ1mycH9r5FCwaNgDA9nB9uU3KWt9m', 'expires_in': 36000, 'token_type': 'Bearer', 'scope': 'accounts.account:change_password accounts.account:update accounts.account:retrieve wallet.wallet:get_balance billboard_merchant.billboard:retrieve billboard_merchant.billboard:update billboard_merchant.billboard:create wallet.user:transaction_list data_crud.data:retrieve data_crud.data:update data_crud.data:delete', 'refresh_token': 'VF4P11tfcnGv9tc2u0qH6035OW5qU4'} client = ApiClient('+989106853582') # client.auth_data = auth_data # print(client.login_as_client_credentials()) print(client.request_otp()) import time start = time.time() print(client.otp_login('77502')) # end = time.time() # print(end - start) # print(client.get_application_preferences()) # exit() # print(client.introspect_account('V3LUQ9OryHOy6q5iWwLBRAtRBm80ex')) # print(client.get_wallet_balance()) # ott = client.get_application_token()[1]['ott'] # print(client.ott_login(ott)) # print(ott) client.update_account('test')