TweePy - how to hide API key

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 15:03:07

问题


I am building simple app which is using Twitter API. What I have to do to hide my Twitter app keys? For example, if I will put my program to the internet and somebody who look up to the code will know my consumer key, access token etc. And if I not include this information into my program, that it won't be work!


回答1:


I'm assuming by putting on the internet you mean publishing your code on github or such.

In that case you should always separate code and configuration. Put your API keys in an .ini file, i.e. config.ini, then load that file from python program using configparser

Add configuration file to your .gitignore so it would not get added to the source control.




回答2:


Assuming you're running on a Unix like system, one way to handle this is environment variables.

In your shell you can do this:

export TWITTER_API_KEY=yoursecretapikey

Note that you don't use quotes of any kind for this.

Then in your script:

import os
twitter_key = os.environ.get('TWITTER_API_KEY')


来源:https://stackoverflow.com/questions/34165172/tweepy-how-to-hide-api-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!