【问题标题】:How to Logout from GMail using Python如何使用 Python 从 GMail 中注销
【发布时间】:2019-02-01 16:32:26
【问题描述】:

我正在尝试构建一个桌面 Python3 应用程序,该应用程序将登录到 GMail 并执行一些任务。我使用了Google Developers Guide 中给出的 quickstart.py 示例代码,它运行良好。我的应用程序登录并检索数据。

但是,在程序执行并终止后,浏览器仍保持登录到 GMail,除非用户专门在其浏览器上打开 GMail 并实际注销。这是一个安全隐患,因为用户可能会忘记注销。

我正在寻找我的 python 程序在程序终止之前自动注销用户的方法。如果有人能指导我如何做到这一点,将不胜感激。

【问题讨论】:

    标签: python-3.x gmail gmail-api logout


    【解决方案1】:

    这非常简单,我明白了from here

    import webbrowser
    webbrowser.open('https://mail.google.com/mail/u/0/?logout&hl=en', new=2)
    

    【讨论】:

      【解决方案2】:

      如果您删除该文件,gmail API 会在 CWD 上创建一个名为“token.pickle”的文件,然后使用您的程序再次登录将需要身份验证。 所以你可以写一个这样的小程序:

      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
      

      打印(“完成”)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        相关资源
        最近更新 更多