Python 怎么在项目文件里隐藏自己的 API 密钥? - V2EX
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 3354 次点击
    这是一个创建于 1439 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    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     Solana     932 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:47 PVG 02:47 LAX 11:47 JFK 14:47
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86