1

I am trying to build a desktop Python3 application that will login to GMail and perform a few tasks. I have used the quickstart.py sample code given in the Google Developers Guide and it works perfectly. My application logs in and retrieves data.

However, after the program executes and terminates, the browser remains logged into GMail, unless the user specifically opens GMail on his browser and actually logsout. This is a security hazard as the user may forget to logout.

I am looking for way in which my python program will automatically log the user out before the program terminates. Will be grateful if someone can guide me on how this can be done.

Calcutta
  • 1,021
  • 3
  • 16
  • 36

3 Answers3

1

The gmail API creates a file on CWD named "token.pickle" if you delete the file then logging in again with your program will require authentication. So you can write a short program like this :

import os
files = os.listdir(".") # path to where credentials is stored
for file in files:
if file == 'token.pickle':
    choice = input("""
     Do you want to delete {}
     1) yes.
     2) no """.format(file))
    if str(choice) == '1':
            os.remove(file)
            break

print("Done")

0

this is absurdly simple and i got the idea from here.

import webbrowser
webbrowser.open('https://mail.google.com/mail/u/0/?logout&hl=en', new=2)
Calcutta
  • 1,021
  • 3
  • 16
  • 36
0

As an updated answer for @Michael Gregory's solution, the file now is called: token_gmail_v1.pickle

so you it's now:

os.remove("./token_gmail_v1.pickle") # or -> os.remove("./*.pickle")
 
# as long as it's the only .pickle file 
# in your working directory since this method is a dangerous one.

Instead of

os.remove("./token.pickle")



Side note: You can use it to check if the user is connected as well.

import os.path

isConnected= os.path.isfile("./token_gmail_v1.pickle")
black-purple
  • 29
  • 1
  • 3