async def generate_credentials(gdrive): """ - Generate credentials - """ configs = { "installed": { "client_id": G_DRIVE_CLIENT_ID, "client_secret": G_DRIVE_CLIENT_SECRET, "auth_uri": GOOGLE_AUTH_URI, "token_uri": GOOGLE_TOKEN_URI, } } creds = None if G_DRIVE_AUTH_TOKEN_DATA is not None: """ - Repack credential objects from strings - """ creds = pickle.loads( codecs.decode(G_DRIVE_AUTH_TOKEN_DATA.encode(), "base64")) else: if isfile("auth.txt"): with open("auth.txt", "r") as token: creds = token.read() creds = pickle.loads(codecs.decode(creds.encode(), "base64")) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_config( configs, SCOPES, redirect_uri=REDIRECT_URI) auth_url, _ = flow.authorization_url( access_type='offline', prompt='consent') async with gdrive.client.conversation(BOTLOG_CHATID) as conv: await conv.send_message( "Please go to this URL:\n" f"{auth_url}\nauthorize then reply the code" ) r = conv.wait_event( events.NewMessage(outgoing=True, chats=BOTLOG_CHATID)) r = await r code = r.message.message.strip() flow.fetch_token(code=code) creds = flow.credentials """ - Unpack credential objects into strings - """ if G_DRIVE_AUTH_TOKEN_DATA is None: with open("auth.txt", "w") as f: pickled = codecs.encode( pickle.dumps(creds), "base64").decode() """ - Put into file to use it later - """ f.write(pickled) await gdrive.client.send_file( BOTLOG_CHATID, "auth.txt", caption=("This is your `G_DRIVE_AUTH_TOKEN_DATA`, " "open then copy and paste to your heroku configvars." "\nor you can do >`set var G_DRIVE_AUTH_TOKEN_DATA " "<value inside auth.txt>`") ) return creds
async def services(gdrive): """ Generate credentials """ await gdrive.edit("`Setting information...`") configs = { "installed": { "client_id": G_DRIVE_CLIENT_ID, "client_secret": G_DRIVE_CLIENT_SECRET, "auth_uri": GOOGLE_AUTH_URI, "token_uri": GOOGLE_TOKEN_URI, } } creds = None if G_DRIVE_AUTH_TOKEN_DATA is not None: # decoded creds string to class object creds = pickle.loads( codecs.decode(G_DRIVE_AUTH_TOKEN_DATA.encode(), "base64")) else: if isfile("auth.txt"): with open("auth.txt", "r") as token: creds = token.read() creds = pickle.loads(codecs.decode(creds.encode(), "base64")) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_config( configs, SCOPES, redirect_uri=REDIRECT_URI) auth_url, _ = flow.authorization_url( access_type='offline', prompt='consent') async with gdrive.client.conversation(BOTLOG_CHATID) as conv: await conv.send_message( "Please go to this URL:\n" f"{auth_url}\nauthorize then reply the code" ) r = conv.wait_event( events.NewMessage(outgoing=True, chats=BOTLOG_CHATID)) r = await r code = r.message.message.strip() flow.fetch_token(code=code) creds = flow.credentials if G_DRIVE_AUTH_TOKEN_DATA is None: with open("auth.txt", "w") as f: # save credentials object as a string pickled = codecs.encode( pickle.dumps(creds), "base64").decode() f.write(pickled) service = build('drive', 'v3', credentials=creds, cache_discovery=False) return service
async def create_app(gdrive): """ - Create google drive service app - """ creds = None if G_DRIVE_AUTH_TOKEN_DATA is not None: """ - Repack credential objects from strings - """ creds = pickle.loads( base64.b64decode(G_DRIVE_AUTH_TOKEN_DATA.encode())) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: await gdrive.edit("`Refreshing credentials...`") """ - Refresh credentials - """ creds.refresh(Request()) else: await gdrive.edit("`Credentials is empty, please generate it...`") return False service = build('drive', 'v3', credentials=creds, cache_discovery=False) return service
async def generate_credentials(gdrive): """ - Generate credentials - """ error_msg = ( "`[TOKEN - ERROR]`\n\n" " • `Status :` **RISK**\n" " • `Reason :` There is data corruption or a security violation.\n\n" "`It's probably your` **G_DRIVE_TOKEN_DATA** `is not match\n" "Or you still use the old gdrive module token data!.\n" "Please change it, by deleting` **G_DRIVE_TOKEN_DATA** " "`from your ConfigVars and regenerate the token and put it again`." ) configs = { "installed": { "client_id": G_DRIVE_CLIENT_ID, "client_secret": G_DRIVE_CLIENT_SECRET, "auth_uri": GOOGLE_AUTH_URI, "token_uri": GOOGLE_TOKEN_URI, } } creds = None try: if G_DRIVE_AUTH_TOKEN_DATA is not None: """ - Repack credential objects from strings - """ try: creds = pickle.loads( codecs.decode(G_DRIVE_AUTH_TOKEN_DATA.encode(), "base64")) except pickle.UnpicklingError: return await gdrive.edit(error_msg) else: if isfile("auth.txt"): """ - Load credentials from file if exists - """ with open("auth.txt", "r") as token: creds = token.read() try: creds = pickle.loads( codecs.decode(creds.encode(), "base64")) except pickle.UnpicklingError: return await gdrive.edit(error_msg) except binascii.Error as e: return await gdrive.edit( "`[TOKEN - ERROR]`\n\n" " • `Status :` **BAD**\n" " • `Reason :` Invalid credentials or token data.\n" f" -> `{str(e)}`\n\n" "`if you copy paste from 'auth.txt' file and still error " "try use MiXplorer file manager and open as code editor or " "if you don't want to download just run command`\n" ">`.term cat auth.txt`\n" "Cp and paste to `G_DRIVE_AUTH_TOKEN_DATA` heroku ConfigVars or\n" ">`.set var G_DRIVE_AUTH_TOKEN_DATA <token you get>`\n\n" "Or if you still have value from old module remove it first!, " "because my module use v3 api while the old is using v2 api...\n" ">`.del var G_DRIVE_AUTH_TOKEN_DATA` to delete the old token data." ) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: await gdrive.edit("`Refreshing credentials...`") """ - Refresh credentials - """ creds.refresh(Request()) else: """ - Create credentials - """ await gdrive.edit("`Creating credentials...`") flow = InstalledAppFlow.from_client_config( configs, SCOPES, redirect_uri=REDIRECT_URI) auth_url, _ = flow.authorization_url( access_type='offline', prompt='consent') msg = await gdrive.respond( "`Go to your BOTLOG chat to authenticate`" " **G_DRIVE_AUTH_TOKEN_DATA**" ) async with gdrive.client.conversation(BOTLOG_CHATID) as conv: await conv.send_message( "Please go to this URL:\n" f"{auth_url}\nauthorize then reply the code" ) r = conv.wait_event( events.NewMessage(outgoing=True, chats=BOTLOG_CHATID)) r = await r code = r.message.message.strip() flow.fetch_token(code=code) creds = flow.credentials await gdrive.client.delete_messages(gdrive.chat_id, msg.id) """ - Unpack credential objects into strings - """ if G_DRIVE_AUTH_TOKEN_DATA is None: with open("auth.txt", "w") as f: pickled = codecs.encode( pickle.dumps(creds), "base64").decode() """ - Put into file to use it later - """ f.write(pickled) await gdrive.client.send_file( BOTLOG_CHATID, "auth.txt", caption=("This is your `G_DRIVE_AUTH_TOKEN_DATA`, " "open then copy and paste to your heroku ConfigVars, " "or do:\n>`.set var G_DRIVE_AUTH_TOKEN_DATA " "<value inside auth.txt>`") ) msg = await gdrive.respond( "`Go to your BOTLOG chat to get` **G_DRIVE_AUTH_TOKEN_DATA**\n" "`The next time you called the command you didn't need to " "authenticate anymore as long there is a valid file 'auth.txt'" " or, you already put the value from 'auth.txt'" " to your heroku app ConfigVars.` **G_DRIVE_AUTH_TOKEN_DATA**" ) await asyncio.sleep(3.5) await gdrive.client.delete_messages(gdrive.chat_id, msg.id) return creds