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

2021-11-03 23:57:18 +08:00
 MiketsuSmasher

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

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一般怎么保存(或者隐藏)?

2854 次点击
所在节点    Python
7 条回复
lfzyx
2021-11-04 00:01:34 +08:00
用环境变量
ipwx
2021-11-04 00:03:05 +08:00
.gitignore

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

至于怎么在服务端存取,我见过有人用环境变量的
MiketsuSmasher
2021-11-04 00:07:20 +08:00
@CEBBCAT 懂了,就是说这种活根本不应该在用户那里做,不过我手头没有云服务之类的东西
ysc3839
2021-11-04 01:39:33 +08:00
没记错的话微软的 API 配置为 Public client 就不需要 secret ,不过 redirect URI 有限制。
imn1
2021-11-04 13:23:50 +08:00
所有账密相关都应外部导入(文件放在项目外),设置一个 config 变量让用户自己填写
放在项目里面,或者写入代码,都是自用方便而已,不是给客户用的
akriafly01
2021-12-27 10:18:22 +08:00
变量放在.env 里,然后把这个文件放到.gitignore 里就行了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/812886

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX