管理oauth访问令牌的python库
stups-tokens的Python项目详细描述
一个python库,它将oauth 2.0服务访问令牌保存在内存中供您使用。
安装
$ sudo pip3 install --upgrade stups-tokens
用法
importrequestsimporttimeimporttokens# will use OAUTH2_ACCESS_TOKEN_URL environment variable by default# will try to read application credentials from CREDENTIALS_DIRtokens.configure(url='https://example.com/access_tokens')tokens.manage('example',['read','write'])tokens.start()tok=tokens.get('example')requests.get('https://example.org/',headers={'Authorization':'Bearer {}'.format(tok)})time.sleep(3600)# make the token expiretok=tokens.get('example')# will refresh the expired tokenrequests.get('https://example.org/',headers={'Authorization':'Bearer {}'.format(tok)})
此库还允许直接从文件读取令牌。令牌需要使用文件名${CREDENTIALS_DIR}/${TOKEN_NAME}-secret:
importtokens# the environment variable CREDENTIALS_DIR must be set correctlytokens.configure(from_file_only=True)tokens.manage('full-access')tok=tokens.get('full-access')requests.get('https://example.org/',headers={'Authorization':'Bearer {}'.format(tok)})
本地测试
“tokens”库允许通过oauth2访问令牌环境变量注入固定的oauth2访问令牌。 这允许在本地使用带有个人OAuth2令牌的库测试应用程序(例如,由“zign”生成):
$ MY_TOKEN=$(zign token -n mytok) $ exportOAUTH2_ACCESS_TOKENS=mytok=$MY_TOKEN $ ./myapp.py # start my local Python app using the tokens library
释放
正在将新版本上载到pypi:
$ ./release.sh <NEW-VERSION>