首页 > 解决方案 > 我可以使用 GOOGLE_APPLICATION_CREDENTIALS 代替特定的邮件设置吗

问题描述

我正在尝试通过 Flask-security 设置电子邮件确认,我想知道是否可以使用从https://console.cloud.google.com/apis/credentials/serviceaccountkey获得的 GOOGLE_APPLICATION_CREDENTIALS 而不是设置邮件设置(MAIL_SERVER、MAIL_PASSWORD 等)。

这是我的代码:

class BaseConfig:
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_RECORD_QUERIES = False
SQLALCHEMY_EXPIRE_ON_COMMIT = False

MARSHMALLOW_STRICT = True
MARSHMALLOW_DATEFORMAT = 'rfc'

SECRET_KEY = 'secret_key'
SECURITY_LOGIN_SALT = 'test'
SECURITY_PASSWORD_HASH = 'pbkdf2_sha512'
SECURITY_TRACKABLE = True
SECURITY_PASSWORD_SALT = 'something'
WTF_CSRF_ENABLED = False
SECURITY_REGISTERABLE = True
SECURITY_CONFIRMABLE = True
SECURITY_RECOVERABLE = True
SECURITY_TOKEN_AUTHENTICATION_HEADER = 'Authorization'
MAX_AGE = 86400
GOOGLE_APPLICATION_CREDENTIALS = '\google_credentials_etc.json' 

谢谢你。

标签: python-3.xflaskgoogle-cloud-platformflask-securityflask-mail

解决方案


GOOGLE_APPLICATION_CREDENTIALS is used by the Google Client libraries, and only then, when set as an environment variable. It has no bearing on Flask, nor will the client libraries automatically pick it up from a Flask configuration.

You could json.loads the service account file and then extract the email, but there's no service information or relay server information in the credentials file.

More importantly, flask-mail doesn't connect over oauth2. It uses SMTP. I'm pretty Gmail relays are also SMTP. So even if you somehow got an oauth2 token from the service account, you wouldn't be able to use it to connect to the relay server.


推荐阅读