import json import time import requests OAUTH_CLIENT_ID = 'WNQzBFHxell95Px45veRncL2vIw6l90Q4Zr5yKuU' OAUTH_CLIENT_SECRET = 'pbkdf2_sha256$390000$cu9a2xBKZzR5sLfcBNsRB6$UZ6MurBq8mfccWDdCCvNTCv3EyiAKSGkh51l2rlAYVk=' # OAUTH_CLIENT_ID = 'zaKGaL7IpjRHzfzjbmcXIUAlPIPCf3ZNsqKuBh1t' # OAUTH_CLIENT_ID = 'zaKGaL7IpjRHzfzjbmcXIUAlPIPCf3ZNsqKuBh1t' # OAUTH_CLIENT_SECRET = 'pbkdf2_sha256$390000$wyByMVckbY9beSRc9ZXNBo$cN5f17TAV13FD+1ioRQ1ANnNOVCd31JJbIrmswxFxA4=' # OAUTH_CLIENT_SECRET = 'pbkdf2_sha256$390000$aAYjehfEm4Jw8FWU1cV5so$OIzTz03dHupFypxfjm4wCBw/J86Bbd4RlmhpMrxo8Bo=' API_URI = 'https://accounts.mindplus-dev.ir' 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 education.course:retrieve 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 ', "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 ', "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) return response and 'code' in response, response client = ApiClient('+989106853582') # print(client.login_as_client_credentials()) print(client.request_otp()) print(client.otp_login('12345')) print(client.update_account('Sayyid Hamid')) # ott = client.get_application_token()[1]['ott'] # print(client.ott_login(ott)) # print(ott)