跳转到主要内容

用于管理OAuth访问令牌的Python库

项目描述

Build Status Code Coverage PyPI Downloads Latest PyPI version License

一个Python库,用于在内存中存储OAuth 2.0服务访问令牌供您使用。

安装

$ sudo pip3 install --upgrade stups-tokens

用法

import requests
import time
import tokens

# will use OAUTH2_ACCESS_TOKEN_URL environment variable by default
# will try to read application credentials from CREDENTIALS_DIR
tokens.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 expire

tok = tokens.get('example') # will refresh the expired token
requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

此库还允许直接从文件读取令牌。令牌需要存储在文件名 ${CREDENTIALS_DIR}/${TOKEN_NAME}-secret

import tokens

# the environment variable CREDENTIALS_DIR must be set correctly
tokens.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_ACCESS_TOKENS 环境变量注入固定的OAuth2访问令牌。这允许使用个人OAuth2令牌(例如由“zign”生成的令牌)在本地测试使用此库的应用程序

$ MY_TOKEN=$(zign token -n mytok)
$ export OAUTH2_ACCESS_TOKENS=mytok=$MY_TOKEN
$ ./myapp.py # start my local Python app using the tokens library

发布

将新版本上传到PyPI

$ ./release.sh <NEW-VERSION>

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

stups-tokens-1.1.19.tar.gz (4.5 kB 查看哈希值)

上传时间

构建分发

stups_tokens-1.1.19-py3-none-any.whl (6.1 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面