V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Nov 3, 2021 · 3575 views
    This topic created in 1638 days ago, the information mentioned may be changed or developed.

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

    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 replies    2021-12-27 10:18:22 +08:00
    lfzyx
        1
    lfzyx  
       Nov 4, 2021
    用环境变量
    ipwx
        2
    ipwx  
       Nov 4, 2021
    .gitignore

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

    至于怎么在服务端存取,我见过有人用环境变量的
    MiketsuSmasher
        4
    MiketsuSmasher  
    OP
       Nov 4, 2021
    @CEBBCAT 懂了,就是说这种活根本不应该在用户那里做,不过我手头没有云服务之类的东西
    ysc3839
        5
    ysc3839  
       Nov 4, 2021 via Android
    没记错的话微软的 API 配置为 Public client 就不需要 secret ,不过 redirect URI 有限制。
    imn1
        6
    imn1  
       Nov 4, 2021
    所有账密相关都应外部导入(文件放在项目外),设置一个 config 变量让用户自己填写
    放在项目里面,或者写入代码,都是自用方便而已,不是给客户用的
    akriafly01
        7
    akriafly01  
       Dec 27, 2021
    变量放在.env 里,然后把这个文件放到.gitignore 里就行了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5706 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 01:59 · PVG 09:59 · LAX 18:59 · JFK 21:59
    ♥ Do have faith in what you're doing.