V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MiketsuSmasher
V2EX  ›  Python

Python 怎么在项目文件里隐藏自己的 API 密钥?

  •  
  •   MiketsuSmasher · 2021-11-03 23:57:18 +08:00 · 2822 次点击
    这是一个创建于 876 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在做一个启动器,做到使用微软账号登录的部分,看到这样一段:

    The next step is to get an access token from the auth code. This isn't done in the browser for security reasons.

    This exchange should be done on the server-side as it requires the use of your client secret which, as the name implies, should be kept secret.

    POST https://login.live.com/oauth20_token.srf
     client_id=<your client id>
     &client_secret=<your client secret>
     &code=<auth code / the code from step 1>
     &grant_type=authorization_code
     &redirect_uri=<your redirect uri>
    

    像这种需要client_secret才能使用的认证服务,client_secret一般怎么保存(或者隐藏)?

    7 条回复    2021-12-27 10:18:22 +08:00
    lfzyx
        1
    lfzyx  
       2021-11-04 00:01:34 +08:00
    用环境变量
    ipwx
        2
    ipwx  
       2021-11-04 00:03:05 +08:00
    .gitignore

    去掉一个配置文件
    CEBBCAT
        3
    CEBBCAT  
       2021-11-04 00:03:36 +08:00
    这段英文的意思应该是在说:
    不要在浏览器里完成 auth code -> access token 的操作,应该在服务端去做,这样才比较安全。
    而之所以需要在服务端去做,是因为这个过程中无可避免地需要使用到 client_secret ,就如同这个 secret 的意思,它应该被保密。

    至于怎么在服务端存取,我见过有人用环境变量的
    MiketsuSmasher
        4
    MiketsuSmasher  
    OP
       2021-11-04 00:07:20 +08:00
    @CEBBCAT 懂了,就是说这种活根本不应该在用户那里做,不过我手头没有云服务之类的东西
    ysc3839
        5
    ysc3839  
       2021-11-04 01:39:33 +08:00 via Android
    没记错的话微软的 API 配置为 Public client 就不需要 secret ,不过 redirect URI 有限制。
    imn1
        6
    imn1  
       2021-11-04 13:23:50 +08:00
    所有账密相关都应外部导入(文件放在项目外),设置一个 config 变量让用户自己填写
    放在项目里面,或者写入代码,都是自用方便而已,不是给客户用的
    akriafly01
        7
    akriafly01  
       2021-12-27 10:18:22 +08:00
    变量放在.env 里,然后把这个文件放到.gitignore 里就行了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3584 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:37 · PVG 12:37 · LAX 21:37 · JFK 00:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.