Skip to main content
 首页 » 编程设计

python之不显示密码的电子邮件和密码验证

2025年05月04日30zhujiabin

我发现自己需要登录到 google 才能使用远程 api,但是它需要电子邮件和密码来验证该过程。无论如何我可以隐藏它们并要求项目的其他贡献者添加他们自己的(安全地)?

from google.appengine.ext.remote_api import remote_api_stub 
import database 
import getpass 
 
email = "gmail@gmail.com" #can't show this line 
password = "password" #can't show this line 
 
def auth_func(): 
  return (email, password) 
 
remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', auth_func, 
                               'app.appspot.com') 

请您参考如下方法:

您可以将敏感信息保存在环境变量中,并在运行应用程序之前在应用程序文档中包含设置环境变量的说明。

要从 Python 中检索变量(来自 here ):

import os 
 
email = os.environ.get("GOOGLE_EMAIL") 
password = os.environ.get("GOOGLE_PASSWORD") 
 
if email and password: 
    # ... process... 

如果您正在使用 virtualenv(您应该使用!),您可能还想使用类似 autoenv 的工具(或在 this 问题中提到的其他人)当您 cd 进入项目目录时,它可以为您的单个命令行/控制台 session 设置相关的环境变量。

您也可以将命令放在 shell 脚本/批处理文件中,并确保不要提交它。